【问题标题】:rails link_to body with action parameter?带有动作参数的rails link_to body?
【发布时间】:2018-02-04 06:29:28
【问题描述】:

我有一个文章表和一个回复表。 带有“reply_count”列的文章。在视图中,我想在我的 link_to 中显示回复计数,但我不知道该怎么做。

这里是控制器代码

class ArticleController < ApplicationController

  def index
    @article = Article.all
  end

end

这里是文章视图

<%= link_to 'Reply(<%= article.replies_count %>)', path %>

希望可以这样显示

<a href=path> Reply(2) </a>

需要大家帮忙,谢谢...

【问题讨论】:

  • 我输入了错误的单词,reply_count 应该是replies_count,但问题无关紧要。请忽略它

标签: ruby-on-rails link-to


【解决方案1】:

字符串插值是关键字。请注意双引号,它将使#{} 有效

&lt;%= link_to "Reply #{article.replies_count}", path %&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多