views:

70

answers:

2

I'm trying to put together a form_tag that edits several Shift objects. I have the form built properly, and it's passing on the correct parameters. I have verified that the parameters work with updating the objects correctly in the console. However, when I click the submit button, I get the error:

ActiveRecord::RecordNotFound in ShiftsController#update_individual
Couldn't find Shift without an ID

My route for the controller it is calling looks like this looks like this:

map.resources :shifts, :collection => { :update_individual => :put }

The method in ShiftsController is this:

def update_individual
  Shift.update(params[:shifts].keys, params[:shifts].values)
  flash[:notice] = "Schedule saved"
end

The relevant form parts are these:

<% form_tag( update_individual_shifts_path ) do %> 
  ... (fields for...)
  <%= submit_tag "Save" %>  
<% end %>

Why is this not working? If I browse to the url: "http://localhost:3000/shifts/update_individual/5" (or any number that corresponds to an existing shift), I get the proper error about having no parameters set, but when I pass parameters without an ID of some sort, it errors out.

How do I make it stop looking for an ID at the end of the URL?

+1  A: 

I think that you need to tell the form tag helper you want to use PUT instead of POST

<% form_tag( update_individual_shifts_path, :method => :put) do %>
  ... fields ....
  <%= submit_tag "Save" %>
<% end %>
Corey
Nope, I'd tried that, and just retried it and still no luck. Thank you, though.
alkaloids
And additionally, it is already getting called with PUT because that's how it's mapped in the routes.rb, as shown above. =/
alkaloids
Oh really, that's a drag, maybe Shift.update doesn't like the format of params[:shifts] hash?
Corey
Yeah, it's really weird. I have tried just copy/pasting the params[:shifts] hash from the trace when it dumps into console, and Shift.update handles the hash fine. I spent a long time thinking that was the problem before realizing it was a routing issue...
alkaloids
A: 

Amazingly, it turns out that I was able to fix this by a combination of renaming the method and passing a dummy variable. Changes were to the lines:

form.html.erb:

<% form_tag( poop_individual_shifts_path ) do %>

routes.rb:

map.poop_individual_shifts "poop_shifts", :controller => 'shifts', :action => "poop_individual", :method => "put", :id => 4
map.resources :shifts 

There I pass it an ID of 4 every time, it doesn't matter, it's not actually doing anything with the shift object it goes and grabs, it's just ... I don't know, a hack, I guess.

shifts_controller.rb:

def poop_individual
alkaloids
So the actual real reason for all that jazz was that I was using filter_resource_access in the method that was trying to force it to act RESTfully. Oops. If I changed the authorization a touch, then I didn't have to pass the dummy variable.
alkaloids