0

The REST route for EDIT is overriding the first URL parameter in addition to the last URL parameter but other parameters are unaltered.

How do I stop the form from changing the url first parameter?

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 page form has action:

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

why is :tid being updated to :rid and how do i prevent it.

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

has the same problem with :tid being updated to match :pid but all other params are intact.

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

How do i ensure the edit page routes do not change the :tid parameter in the URL

  • why do you need the :tid when editing the round? second, you could just pass in the @tournament object in your form. – BenKoshy Mar 25 '19 at 03:33
  • try this: https://stackoverflow.com/a/46919792/4880924 – BenKoshy Mar 25 '19 at 03:34
  • I use the :tid parameter to display the tournament information on the page that shows the round. specifically i am displaying the tournament name on the form. I am using this method so that i can display the tournament information on ever page without having a reference table for every model. Do you mean i should pass in the tournament object instead of using :tid or that passing the @tournament object will prevent the url from replacing :tid ? – Chris Thompson Mar 25 '19 at 04:00
  • "passing the `@tournament` object will prevent the url from replacing :tid " ---> How will your form know the tournament you are dealing with unless you pass in the :tid or the `@tournament` object? Read stackoverflow.com/a/46919792/4880924 for more info. Also please note that you might want to consider using shallow nesting of your routes: https://edgeguides.rubyonrails.org/routing.html#shallow-nesting – BenKoshy Mar 25 '19 at 04:21
  • I am passing the tournament object in my rounds_controller def edit @tournament = Tournament.find(params[:tid]) end I don't know how else to pass it to the form. – Chris Thompson Mar 25 '19 at 04:29
  • I am looking at the shallow nesting and seeing if that would fix the issue also. – Chris Thompson Mar 25 '19 at 04:34
  • "I am passing the tournament object in my rounds_controller def edit @tournament = Tournament.find(params[:tid]) end I don't know how else to pass it to the form" - you have to pass it directly - see here: stackoverflow.com/a/46919792/4880924 – BenKoshy Mar 25 '19 at 04:36
  • Thanks BKSpurgeon, I couldn't make the non-shallow nesting work, but i believe i can get it working with shallow routes like you suggested. – Chris Thompson Mar 25 '19 at 06:08

1 Answers1

0

Remove all the routes to tournaments, rounds, matches and points and use resources routes

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

Then check if it works.

Amit Patel
  • 15,609
  • 18
  • 68
  • 106
  • This did prevent the edit page from altering the first parameter however, it will require a refactor of all my links AND since i was using the form-helper to make my new and edit form a rendered partial and using the nested routes requires passing the URL seperate in the form I lost the ability to keep my form a partial for both new and edit. SOOO i decided to do ,shallow: true with the above and refactor every link in my app and some of the controller logic since i will no longer have the parent.parent_id in the url params. – Chris Thompson Mar 25 '19 at 06:04