Something weird’s happening with my Rails app. When I try to send an update command to one of my controllers, for some reason the submit button appears to be changing some of the params.
I’m using nested resources and setting up races which have participants. Pretty simple. For some reason, though, when I try to update a participant, it changes the value of :race_id to the participant’s id (:id).
Though it only does that on update. I seem to be able to create new participants perfectly using the very same form, and so the very same submit button.
Here is some of the relevant code:
_form.rb (used by new and update)
<%= form_for ([:race, @participant]) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p><%= f.submit %></p>
<% end %>
participants_controller.rb
class ParticipantsController < ApplicationController
before_filter :authenticate_user!, :only => [:edit, :update, :destroy, :create, :new]
before_filter :set_up_race
...
def create
@participant = @race.participants.new(params[:participant])
if @participant.save
redirect_to setup_race_path(@race), :notice => "Successfully created participant."
else
render :action => 'new'
end
end
...
def update
@participant = Participant.find(params[:id])
if @participant.update_attributes(params[:participant])
redirect_to setup_race_path(@race), :notice => "Successfully updated participant."
else
render :action => 'edit'
end
end
...
def set_up_race
@race = Race.find(params[:race_id])
end
end
Here’s why it seems that parameters are being changed:
If I modify the _form.rb file to include <%= params[:race_id] %>
The screen tells me 3
When I click submit I get:
Couldn’t find Race with id=25
Request
Parameters:
{“utf8″=>”✓”, “_method”=>”put”,
“authenticity_token”=>”4VCZP9sI/iv8n454I8AE76n5vLiwGayuXc1NrPYfzGc=”,
“participant”=>{“name”=>”hgdjhgf”}, “commit”=>”Update Participant”,
“race_id”=>”25”, “id”=>”25”}
(As you can see under parameters, “race_id”=>”25″ after I click submit, but the page originally had :race_id =>”3” (proven above, and again if I do anything to make the form crash)).
So the question is, after all of that, why is :race_id changing somewhere between the view and the controller?
EDIT: here’s a rake routes output, as per Jeff’s request:
http://dylancodes.net/personal/ARTk/images/routes.png
What does the url look like that your form is posting to? Based on your routes, I would expect it to look something like
/races/3/participants/25. From what I’ve read, the linecreates a namespaced route (search for “namespaced” on that page) that would look like
/races/participants/25.Does changing that line to this work for you?
That should build the form url as
/races/:race_id/participants/:id.