link_to和url一起发送参数,并把它们抓在目标页面上

我怎样才能有一个页面上的链接,使用户到另一个url,并传递一个参数和目标url我们如何拿起这个参数。

通常我会添加如下链接:

<%= link_to "Add Product", '/pages/product' %> 

但是,我怎么能发送参数与此​​url? 我可以通过using params[:parm_name]在目标操作中select它们using params[:parm_name]

只需将它们添加到链接:

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

并在控制器中:

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

如果你使用helper方法来处理路由(例如company_path ),那么你可以添加params的哈希,所以这两个应该是类似的:

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

从文档 :

 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>