Ruby on Rails - Passing values to a link_to

link-to, parameters, ruby-on-rails, url, url-parameters

Solution

If you want to pass arbitrary parameters to the following call

<%= link_to product.name, product %>

you need to invoke the path method explicitly instead of using the implicit version. The line above is equivalent to

<%= link_to product.name, product_path(product) %>

Then you can pass parameters

<%= link_to product.name, product_path(product, :from => 'whatever', :to => 'whatever') %>

Problem

I'm a RoR beginner and am using Rails 3.2.3. I have a search form on my page and it performs a get request and filters the results correctly. I want to add the possibility of also searching Products by dates. I inserted a date_select and am able select the date and when the page refreshes after the search, the chosen dates are still there on the date_select, as I am able to get them through `params`. However, my issue is that when the page renders the products, they have a link_to to their show action. My goal is to also pass alongside the url the dates that were selected to perform the search on that link_to. For ex, if the user selects a date of 20-06-2012 to 25-06-2012 it only shows products inserted on that time frame (and all those params are on the url) But the link to show action of each displayed product is only: ``` link_to <%= link_to product.name, product%> ``` which renders `http://localhost:3000/products/24` (por example) what I want to render/show is something like: ``` http://localhost:3000/products/24?from=20-06-2012&to=25-06-2012 ``` The selected dates to perform the search are not stored in the database at this moment, but I will need to get them from the URL on a latter page, therefore, both dates will need to be preserved through 2 different pages before the users fills a form and then those dates are inserted in the DB. Any tips on this? I've searched but all I found was on how to pass variables that exist in the model and I do not want to use cookies nor session variables. Thanks in advance, Regards

Original source