Search code examples
ruby-on-railslink-to

rails link_to body with action parameter?


I have an article table and a reply table. Article with a column 'reply_count'. In view, I want to show replies count in my link_to, but I don't know how to do it.

here's controller code

class ArticleController < ApplicationController

  def index
    @article = Article.all
  end

end

here's article view

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

I hope it can show like this

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

Need everybody's help, thanks...


Solution

  • String interpolation is the keyword. Please notice the double quotes, it will make #{} works

    <%= link_to "Reply #{article.replies_count}", path %>