passing a parameter to the link_to method

JZ. picture JZ. · Mar 26, 2011 · Viewed 7.6k times · Source

How do you pass a parameter through the MVC using the link_to method?

view:

<%= link_to "Remove Tag", remove_tag_issue_path(issue)%>

How do I use the link_to method, to utilize the remove_tag action?

issues_controller.rb

  def remove_tag(parameter)
     @issue.remove_it(parameter)
  end

issue.rb

  def remove_it(parameter)
      self.users.delete(User.find(parameter))
   end

Answer

fl00r picture fl00r · Mar 26, 2011

In controller

def remove_tag
  @issue.remove_it(params[:my_param])
end

And in view

<%= link_to "Remove Tag", remove_tag_issue_path(issue, :my_param => "Hello world")%>