Ruby on rails 在ERB文件中构建url字符串

Ruby on rails 在ERB文件中构建url字符串,ruby-on-rails,ruby,disqus,link-to,Ruby On Rails,Ruby,Disqus,Link To,我想在index.html.erb中显示每篇文章的论文评论数。我需要生成url字符串并将其传递给link_to 所以基本上类似于url=article_path(article.id)+“/#discus_thread” 将显示html生成的url字符串 代码如下: <% @article.each do |article| %> <% link_to article_path(article.id)'/#disqus_thread' %> <-- not w

我想在index.html.erb中显示每篇文章的论文评论数。我需要生成url字符串并将其传递给link_to

所以基本上类似于url=article_path(article.id)+“/#discus_thread”
将显示html生成的url字符串

代码如下:

<% @article.each do |article| %>

  <% link_to article_path(article.id)'/#disqus_thread' %> <-- not working.

<% end %>                


您没有连接字符串,您需要使用
+

link_to(article_path(article.id)+'/#disqus_thread')

此外,正如@engineersmnky所述,在erb中,
运行代码而不呈现返回值。要呈现内容,您的标记需要格式化为

为了清晰起见,您可以这样做:

<%= link_to (article_path(article.id)+ "/#disqus_thread") %>

我不确定我是否理解这一点:

我想在index.html.erb中显示每篇文章的论文评论数

是否希望链接显示为:

21评论

然后是我概述的链接路径?如果是这样的话,应该是:

<%= link_to article.comments.count, (article_path(article.id)+ "/#disqus_thread") %>

所以,您首先需要知道的是,所有url帮助程序最终都会调用url\u。您可以在上找到url的文档

看到这里,您会注意到有一个
:anchor
选项允许您指定url的锚点。因此,正确的“Rails方式”是:

<%= link_to article_path(article, anchor: 'disqus_thread') %>


另一件你似乎忘记了的事情是使用
可能还想提到
啊,事实上,我没听清楚。哦,谢谢。简单。我对链接中的选项参数进行了过度复杂化