Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - Passing values to a link_to

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

like image 849
Silver Avatar asked Jan 17 '23 07:01

Silver


2 Answers

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') %>
like image 196
Simone Carletti Avatar answered Jan 30 '23 21:01

Simone Carletti


Can you try like this

link_to <%= link_to product.name, product, :param_you_want => "value you want" %>

Now you can set value in "param_you_want" as you wish.

But in my case set value in it "value you want"

Hope you can get idea

like image 31
Kashiftufail Avatar answered Jan 30 '23 21:01

Kashiftufail