Alberto
Alberto

Reputation: 467

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

Upvotes: 1

Views: 737

Answers (1)

rb512
rb512

Reputation: 6948

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'.

Upvotes: 1

Related Questions