Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a `link_to` URL passing some custom parameters?

I am using Ruby on Rails 3.0.9 and I would like to generate a link_to URL passing some custom parameters. That is, having a articles_path (www.my_web_site_name.com/articles) I would like to generate something like the following:

link_to 'Sample link title', ... # Here I should implement the code
# => 'http://www.my_web_site_name.com/articles?param1=value1&param2=value2&...

How can I code the link_to statement "a là Ruby on Rails Way" in order to accomplish that? And what if I would_like to 'link_to' an 'article_path(@article)' by passing some custom parameters?

Somewhere I have seen to use something like link_to 'Sample link title', :param1 => 'value1', :param2 => 'value2' but in this case it isn't involved the named route (the generated URL refers to the current path).

like image 593
Backo Avatar asked Aug 12 '11 02:08

Backo


1 Answers

You can send those arguments to the path helper, in this case articles_path. Like this:

link_to 'Sample link title', articles_path(:param1 => 'value1', :param2 => 'value2')

I think that is what you want.

like image 74
DanneManne Avatar answered Oct 05 '22 23:10

DanneManne