简单的方法是将参数传递给 rails link_to?
routes.rb
get '/users/:user_id/accounts/hires/:hire_id/release' => 'hire_milestones#release', as: 'release_hire_milestone'
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 %>
实际的网址是:
http://localhost:3000/users/1/accounts/hires/40
当我将鼠标悬停在按钮上时,它会显示:
http://localhost:3000/users/user_id/accounts/hires/hire_id/release?amount=860
如何向 link_to 的路径添加多个参数?
已解决:
我需要改变:
<%= link_to 'a', release_hire_milestone_path(:user_id, :hire_id, amount: f.bar), class: "tiny button", method: :get %>
到:
<%= link_to 'a', release_hire_milestone_path(params[:user_id], params[:hire_id], amount: f.bar), class: "tiny button", method: :get %>
但是路由有点错误:hire_id
应该是id
最佳答案
除了一些小故障,你的做法是正确的。
您确实传递了参数,但尚未指定值。
您需要做的是传递 user_id
和 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 %>
假设在当前 View 中有可用的 @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 %>
希望这对您有所帮助。
关于ruby-on-rails - 将 URL 参数添加到 link_to 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377900/