ruby on rails - link_to send parameters along with the url and grab them on target page -


how can have link on page takes user url , passes along parameter , on target url how can pick parameter.

usually add links following:

 <%= link_to "add product", '/pages/product' %> 

but how can send parameters along url? can pick them in target action using params[:parm_name]

just add them link:

<%= link_to "add product", '/pages/product?param1=value1&param2=value2' %> 

and in controller:

param1 = params[:param1] # "value1" param2 = params[:param2] # "value2" 

if use helper methods routes (for example company_path), can add hash of params, 2 should similar:

<%= link_to "add product", new_product_path(:param1 => "value1", :param2 => "value2") %> <%= link_to "add product", "/products/new?param1=value1&param2=value2" %> 

from documentation:

link_to "comment wall", profile_path(@profile, :anchor => "wall") # => <a href="/profiles/1#wall">comment wall</a>  link_to "ruby on rails search", :controller => "searches", :query => "ruby on rails" # => <a href="/searches?query=ruby+on+rails">ruby on rails search</a>  link_to "nonsense search", searches_path(:foo => "bar", :baz => "quux") # => <a href="/searches?foo=bar&amp;baz=quux">nonsense search</a> 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -