Syl*_*lar 1 ruby ruby-on-rails ruby-on-rails-4
将参数传递给 rails link_to 的简单方法是什么?
路由文件
get '/users/:user_id/accounts/hires/:hire_id/release' => 'hire_milestones#release', as: 'release_hire_milestone'
Run Code Online (Sandbox Code Playgroud)
html:
<% @foo.map do |f| %>
<b><%= m.reason %></b> at <b>£<%= m.amount_requested %></b>
<span><%= link_to 'a', release_hire_milestone_path(:user_id, :hire_id, amount: f.bar), class: "tiny button", method: :get %></span> <br/>
<% end %>
Run Code Online (Sandbox Code Playgroud)
实际网址是:
http://localhost:3000/users/1/accounts/hires/40
Run Code Online (Sandbox Code Playgroud)
当我将鼠标悬停在按钮上时,它显示:
http://localhost:3000/users/user_id/accounts/hires/hire_id/release?amount=860
Run Code Online (Sandbox Code Playgroud)
如何将多个参数添加到 link_to 的路径?
解决了:
我需要改变:
<%= link_to 'a', release_hire_milestone_path(:user_id, :hire_id, amount: f.bar), class: "tiny button", method: :get %>
Run Code Online (Sandbox Code Playgroud)
到:
<%= link_to 'a', release_hire_milestone_path(params[:user_id], params[:hire_id], amount: f.bar), class: "tiny button", method: :get %>
Run Code Online (Sandbox Code Playgroud)
但路线略有错误:hire_id应该是id
除了一个小故障之外,您正在以正确的方式进行操作。
您确实在传递参数,但尚未指定值。
您需要做的是传递 theuser_id和 the的值hire_id,就像传递amount.
类似于以下内容:
<% @foo.map do |f| %>
<b><%= m.reason %></b> at <b>£<%= m.amount_requested %></b>
<span><%= link_to 'a', release_hire_milestone_path(user_id: <value_of_user_id>, hire_id: <value_of_hire_id>, amount: f.bar), class: "tiny button", method: :get %></span> <br/>
<% end %>
Run Code Online (Sandbox Code Playgroud)
假设在当前视图中有@hire和@user对象可供您使用......并且user_id是@user.id和hire_id是@hire.id,那么上面的内容可以写成如下:
<% @foo.map do |f| %>
<b><%= m.reason %></b> at <b>£<%= m.amount_requested %></b>
<span><%= link_to 'a', release_hire_milestone_path(user_id: @user.id, hire_id: @hire.id, amount: f.bar), class: "tiny button", method: :get %></span> <br/>
<% end %>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
6413 次 |
| 最近记录: |