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

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

Rails create custom function/method and where to store

问题

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

  1. # app/helpers/article_count.rb
  2. module ArticleCountHelper
  3. def display_article_count_string
  4. if Article.count == 1
  5. "This blog has #{Article.public_count} article so far!"
  6. elsif Article.count >= 2
  7. "This blog has #{Article.public_count} articles so far!"
  8. else
  9. "0 articles, so lonely :("
  10. end
  11. end
  12. end

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

  1. <h1>All Article Page</h1>
  2. <%= 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

  1. &lt;h1&gt;All Article Page&lt;/h1&gt;
  2. &lt;% if Article.count == 1%&gt;
  3. This blog has &lt;%= Article.public_count %&gt; article so far!
  4. &lt;% elsif Article.count &gt;= 2 %&gt;
  5. This blog has &lt;%= Article.public_count %&gt; articles so far!
  6. &lt;% else %&gt;
  7. &lt;%= Article.public_count %&gt; articles, so lonely :(
  8. &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:

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

答案1

得分: 3

以下是翻译好的内容:

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

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

  1. module ArticlesHelper
  2. def display_article_count_string
  3. if Article.count == 1
  4. p "这个博客目前有#{Article.public_count}篇文章!"
  5. elsif Article.count >= 2
  6. p "这个博客目前有#{Article.public_count}篇文章!"
  7. else
  8. p "0篇文章,好寂寞 :("
  9. end
  10. end
  11. 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

  1. module ArticlesHelper
  2. def display_article_count_string
  3. if Article.count == 1
  4. p &quot;This blog has #{Article.public_count} article so far!&quot;
  5. elsif Article.count &gt;= 2
  6. p &quot;This blog has #{Article.public_count} articles so far!&quot;
  7. else
  8. p &quot;0 articles, so lonely :(&quot;
  9. end
  10. end
  11. 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

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

在 app/model/article.rb 内部

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

My tips for your case

view/article/index.html.erb

  1. &lt;h1&gt;All Article Page&lt;/h1&gt;
  2. &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

  1. def self.info_count
  2. if Article.count.zero?
  3. &quot;so lonely :(&quot;
  4. else
  5. &quot;this blog has #{Article.count} article so far!
  6. end
  7. end

答案3

得分: 1

Sure, here is the translated content:

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

以及在视图中使用:

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

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

  1. en:
  2. articles:
  3. count:
  4. zero: "没有文章"
  5. one: "1篇文章"
  6. other: "%{count}篇文章"

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

请确保区域设置键是en。

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. It should go to locales like this:

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

  1. and in the view:

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

  1. **Edit:**
  2. 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"

  1. and

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

  1. Be sure that the locale key is en.
  2. Here is the documentation:
  3. https://guides.rubyonrails.org/i18n.html#pluralization
  4. The correct way to do that is with locales instead of methods.
  5. </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:

确定