我如何在页面上有一个链接,将用户带到另一个URL并传递参数和目标网址,我们如何获取该参数.
通常我添加如下链接:
<%= link_to "Add Product", '/pages/product' %>
Run Code Online (Sandbox Code Playgroud)
但是如何与此网址一起发送参数?我可以在目标动作中选择它们吗?using params[:parm_name]
MBO*_*MBO 95
只需将它们添加到链接:
<%= link_to "Add Product", '/pages/product?param1=value1¶m2=value2' %>
Run Code Online (Sandbox Code Playgroud)
在控制器中:
param1 = params[:param1] # "value1"
param2 = params[:param2] # "value2"
Run Code Online (Sandbox Code Playgroud)
如果你对路径使用辅助方法(例如company_path),那么你可以添加参数的散列,所以这两个应该是相似的:
<%= link_to "Add Product", new_product_path(:param1 => "value1", :param2 => "value2") %>
<%= link_to "Add Product", "/products/new?param1=value1¶m2=value2" %>
Run Code Online (Sandbox Code Playgroud)
来自文档:
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&baz=quux">Nonsense search</a>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62556 次 |
| 最近记录: |