简体   繁体   中英

Passing parameters to controller when using Ruby on Rails and link_to helper

I am trying to create a route like this using Ruby on rails:

 get '/products/:store/:destination/:category/'

Sorry if this is trivial, but I'd need to know how build a controller for this, and it would be helpful to know how to pass the listed parameters from the views to the controller using an helper method such as 'link_to'. Thank you very much

You can create the url by passing appropriate attributes :

<%=link_to 'Something Awesome', "/products/#{store}/#{destination}/#{category}"%>

I'm not sure what your use case is and how you plan to pass those parameters in the url. But, you might want to create a form_tag instead for it's a much cleaner solution.

<%form_tag your_awesome_action_path do |f|%>
  <%=text_field_tag :store%>
  <%=text_field_tag :destination%>
  <%=text_field_tag :category%>
  <%=submit_tag 'Submit'%>
<%end%>

where, your_awesome_url is path to the controller action that will process the form. you'll need an action your_awesome_action in the products controller and a corresponding route in routes.rb :

match 'your_awesome_action' => 'products#your_awesome_action'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM