form_with模型改变编辑表单的url参数

问题描述 投票:0回答:1

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

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

意见/轮/ 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' %>

意见/轮/ _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 %>

配置/ 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参数

ruby-on-rails ruby form-helpers
1个回答
0
投票

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

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

然后检查它是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.