form_with модель изменения параметров URL для формы редактирования - PullRequest
0 голосов
/ 25 марта 2019

REST-маршрут для EDIT переопределяет первый параметр 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 / rout.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="✓">

Как я могу гарантировать, что маршруты редактирования страниц не изменяют параметр: tid в URL

1 Ответ

0 голосов
/ 25 марта 2019

Удалите все маршруты на турниры, раунды, матчи и очки и используйте resources маршруты

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

Затем проверьте, работает ли он.

...