passing a parameter to the link_to method

旧城冷巷雨未停 提交于 2020-01-03 17:07:44

问题


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

回答1:


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")%>



回答2:


In the view use the following

<%= link_to 'Remove Tag', remove_tag_issue_path(:issue => @issue)%>

and in controller

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


来源:https://stackoverflow.com/questions/5444651/passing-a-parameter-to-the-link-to-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!