开发者

Routing problem with calling a new method without an ID

开发者 https://www.devze.com 2022-12-25 01:49 出处:网络
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 updati

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[开发者_如何学Go: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?


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 %>


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号