Rails 创建自定义函数/方法以及存储位置

huangapple go评论58阅读模式
英文:

Rails create custom function/method and where to store

问题

在文章索引页面上,我有一个条件来显示文章数量,根据数量不同,'article' 这个词的语法会发生变化。以下是您想要重用的方法或函数的示例,以避免在将来的不同视图中重复编写相同的代码,保持 DRY 原则。您可以将这个方法保存在一个文件中,比如 'article_count.rb',然后在索引视图中调用它:

# app/helpers/article_count.rb
module ArticleCountHelper
  def display_article_count_string
    if Article.count == 1
      "This blog has #{Article.public_count} article so far!"
    elsif Article.count >= 2
      "This blog has #{Article.public_count} articles so far!"
    else
      "0 articles, so lonely :("
    end
  end
end

然后,在您的视图中,您可以这样调用这个方法:

<h1>All Article Page</h1>
<%= display_article_count_string %>

这将帮助您在不同视图中重用相同的逻辑,符合 DRY 原则。这个方法可以保存在一个帮助器文件中,以便在不同视图和控制器中使用。

英文:

On my article index page I have a condition to display the number of articles, the grammar in the word 'article' changes based on the number. This works as current. As below:

view/article/index.html.erb

&lt;h1&gt;All Article Page&lt;/h1&gt;

&lt;% if Article.count == 1%&gt;
This blog has &lt;%= Article.public_count %&gt; article so far!
&lt;% elsif Article.count &gt;= 2 %&gt;
This blog has &lt;%= Article.public_count %&gt; articles so far!
&lt;% else %&gt;
&lt;%= Article.public_count %&gt; articles, so lonely :(
&lt;% end %&gt;

How would I create a custom method or function to apply the above without having to write it all out repeatedly on different views in the future, adhering to DRY. I am aware of partials but want to know whether there is a more rails-like way to do this considering its a highly opinionated system.

After research its not clear whether it should go in my Article model, config/initializer or in my app/controller.

As an example the method would be something like the below, saved within a file called 'article_count.rb'. Then called within the index view, however nothing happens:

def display_article_count_string
  if Article.count == 1
    p &quot;This blog has #{Article.public_count} article so far!&quot;
  elsif Article.count &gt;= 2
    p &quot;This blog has #{Article.public_count} articles so far!&quot;
  else
    p &quot;0 articles, so lonely :(&quot;
  end
end

答案1

得分: 3

以下是翻译好的内容:

对于简单且不太复杂的逻辑,视图逻辑可以放在您的Rails项目的app/helpers中。

对于您的文章控制器,辅助文件将位于app/helpers/articles_helper.rb中。

module ArticlesHelper

  def display_article_count_string
    if Article.count == 1
      p "这个博客目前有#{Article.public_count}篇文章!"
    elsif Article.count >= 2
      p "这个博客目前有#{Article.public_count}篇文章!"
    else
      p "0篇文章,好寂寞 :("
    end
  end

end

对于您希望在不同视图之间共享的更常见的内容,app/helpers/application_helper.rb是另一个地方。

提示:
另一个有用的Rails视图辅助方法是pluralize(1, 'article')方法。它可以帮助提供您资源名称的单数/复数形式更多信息

英文:

For simple and not to complex logic, view logic can go into the app/helpers of your Rails project.

For your articles controller, helper file will live at app/helpers/articles_helper.rb

module ArticlesHelper

  def display_article_count_string
    if Article.count == 1
      p &quot;This blog has #{Article.public_count} article so far!&quot;
    elsif Article.count &gt;= 2
      p &quot;This blog has #{Article.public_count} articles so far!&quot;
    else
      p &quot;0 articles, so lonely :(&quot;
    end
  end

end

For more common stuff you want to share across different views, app/helpers/application_helper.rb is another place.

Tip:
Another useful Rails view helper is the pluralize(1, &#39;article&#39;) method. It can help provide singular/plural form of your resource name More Info

答案2

得分: 1

以下是您要翻译的内容:

我的建议针对您的情况

view/article/index.html.erb

  &lt;h1&gt;所有文章页面&lt;/h1&gt;
  &lt;%= Article.info_count %&gt;
  • Article.info_count 将调用 Article 内的类方法
  • 这是为什么在 article.rb 内部您必须将 self 添加为类方法的原因(请参见下文)

在 app/model/article.rb 内部

  def self.info_count 
    if Article.count.zero? 
      &quot;如此孤独 :(&quot;
    else
      &quot;这个博客到目前为止有 #{Article.count} 篇文章!&quot;
    end
  end
英文:

My tips for your case

view/article/index.html.erb

  &lt;h1&gt;All Article Page&lt;/h1&gt;
  &lt;%= Article.info_count %&gt;
  • Article.info_count will call class method inside Article
  • this is the reason inside article.rb you have to add self as class method (see below)

inside app/model/article.rb

  def self.info_count 
    if Article.count.zero? 
      &quot;so lonely :(&quot;
    else
      &quot;this blog has #{Article.count} article so far!
    end
  end

答案3

得分: 1

Sure, here is the translated content:

应该按照以下方式设置本地化:
```yaml
en:
  article:
    zero: "没有文章"
    one: "1篇文章"
    other: "%{count}篇文章"

以及在视图中使用:

<%= t('.article', count: Article.count) %>

编辑:
在上面的内容中,可能只需要将.article 更改为 article,但请尝试更明确的方式:

en:
  articles:
    count:
      zero: "没有文章"
      one: "1篇文章"
      other: "%{count}篇文章"

<%= t('articles.count', count: Article.count) %>

请确保区域设置键是en。

这里是文档链接:
https://guides.rubyonrails.org/i18n.html#pluralization

正确的做法是使用本地化而不是方法。


<details>
<summary>英文:</summary>

It should go to locales like this:

en:
article:
zero: "No articles"
one: "1 article"
other: "%{count} articles"

and in the view:

<%= t('.article', count: Article.count) %>


**Edit:**
In the above, probably it&#39;s just the `.article` needs to become `article` but try a more explicit way:

en:
articles:
count:
zero: "No articles"
one: "1 article"
other: "%{count} articles"

and

<%= t('articles.count', count: Article.count) %>

Be sure that the locale key is en.

Here is the documentation:
https://guides.rubyonrails.org/i18n.html#pluralization

The correct way to do that is with locales instead of methods.

</details>



huangapple
  • 本文由 发表于 2023年5月6日 19:05:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定