【问题标题】:form_with model altering url parameters for edit formform_with 模型更改编辑表单的 url 参数
【发布时间】:2019-08-15 06:49:54
【问题描述】:

EDIT 的 REST 路由除了最后一个 URL 参数之外,还覆盖了第一个 URL 参数,但其他参数保持不变。

如何阻止表单更改 url 第一个参数?

views/rounds/edit.html.erb

<h1>Editing Round</h1>

<%= render 'form', round: @round %>
<%= link_to 'Show', round_path(params[:tid],params[:rid]) , class: 'btn btn-primary'  %> |
<%= link_to 'Back', tournament_path(params[:tid]), class: 'btn btn-primary' %>

views/rounds/_form.html.erb

<%= form_with(model: round, local: true) do |form| %>

  ...

  <div class="actions">
  <%= form.submit ( form.object.new_record? ? "Create" : "Update"), class: 'btn btn-primary'%>
  </div>

<% end %>

config/routes.rb

get "tournaments/:tid/rounds" => "rounds#index", as: 'rounds'
get "tournaments/:tid/rounds/new" => "rounds#new", as: 'new_round'
post "tournaments/:tid/rounds" => "rounds#create"
delete "tournaments/:tid/rounds/:rid" => "rounds#destroy"
patch "tournaments/:tid/rounds/:rid" => "rounds#update"
put "tournaments/:tid/rounds/:rid" => "rounds#update"
get "tournaments/:tid/rounds/:rid" => "rounds#show", as: 'round'
get "tournaments/:tid/rounds/:rid/edit" => "rounds#edit", as: 'edit_round'



get "tournaments/:tid/rounds/:rid/matches/:mid/points" => "points#index", as: 'points'
get "tournaments/:tid/rounds/:rid/matches/:mid/points/new" => "points#new", as: 'new_point'
post "tournaments/:tid/rounds/:rid/matches/:mid/points" => "points#create"
delete "tournaments/:tid/rounds/:rid/matches/:mid/points/:pid" => "points#destroy"
patch "tournaments/:tid/rounds/:rid/matches/:mid/points/:pid" => "points#update"
put "tournaments/:tid/rounds/:rid/matches/:mid/points/:pid" => "points#update"
get "tournaments/:tid/rounds/:rid/matches/:mid/points/:pid" => "points#show", as: 'point'
get "tournaments/:tid/rounds/:rid/matches/:mid/points/:pid/edit" => "points#edit", as: 'edit_point'

http://localhost:3000/tournaments/1/rounds/2/edit 页面表单有动作:

<form action="/tournaments/2/rounds/2" accept-charset="UTF-8" method="post">

为什么 :tid 被更新为 :rid 以及如何防止它。

http://localhost:3000/tournaments/1/rounds/2/matches/3/points/10/edit

在更新 :tid 以匹配 :pid 但所有其他参数都完好无损时存在同样的问题。

<form action="/tournaments/10/rounds/2/matches/3/points/10" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓">

如何确保编辑页面路由不会更改 URL 中的 :tid 参数

【问题讨论】:

  • 为什么在编辑回合时需要 :tid?其次,您可以在表单中传入@tournament 对象。
  • 我使用 :tid 参数在显示回合的页面上显示锦标赛信息。具体来说,我在表格上显示了比赛名称。我正在使用这种方法,以便我可以在每个页面上显示锦标赛信息,而无需为每个模型提供参考表。你的意思是我应该传入锦标赛对象而不是使用 :tid 还是传递 @tournament 对象会阻止 url 替换 :tid ?
  • "传递 @tournament 对象将阻止 url 替换 :tid " ---> 除非您传递 :tid 或 @987654331,否则您的表单将如何知道您正在处理的锦标赛@ 目的?阅读 stackoverflow.com/a/46919792/4880924 了解更多信息。另请注意,您可能需要考虑使用路由的浅嵌套:edgeguides.rubyonrails.org/routing.html#shallow-nesting
  • 我在 rounds_controller def edit @tournament = Tournament.find(params[:tid]) end 我不知道如何将它传递给表单。

标签: ruby-on-rails ruby form-helpers


【解决方案1】:

删除所有通往锦标赛、回合、比赛和积分的路线,并使用resources路线

resources :tournaments do
  resources :rounds do
    resources :matches do
      resources :points
    end
  end
end

然后检查它是否有效。

【讨论】:

  • 这确实阻止了编辑页面更改第一个参数,但是,它需要重构我的所有链接,并且因为我使用表单助手使我的新表单和编辑表单呈现为部分和使用嵌套路由需要以表单的形式单独传递 URL,我失去了将表单保持为新的和编辑的部分的能力。太棒了,我决定这样做,浅:对上述内容进行 true 并重构我的应用程序中的每个链接和一些控制器逻辑,因为我将不再在 url 参数中包含 parent.parent_id。
猜你喜欢
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
相关资源
最近更新 更多