开发者

Rails Ajax CRUD: Update is PUTing to "/models" and not "/model/:id"

开发者 https://www.devze.com 2023-03-30 05:24 出处:网络
I have a form partial that is rendered on a page: <%= form_for @shift, :remote => true, :html => { :id => \'entry_form\' } do |f| %>

I have a form partial that is rendered on a page:

<%= form_for @shift, :remote => true, :html => { :id => 'entry_form' } do |f| %>
... entry fields ...
<%= f.submit %>

The other partial rendered on the page is a list of the shifts for that day. Create and Delete work just fine, and are pretty standard. For instance, I use the simplest Ajax approach for create:

page.replace_html('listing', render(:partial => 'listing',开发者_如何学C :object => @schedule ))

(Note @schedule has_many @shifts)

The issue arises with Edit and Update. Hitting 'Edit' would effectively populate my form partial with Ajax, but when I hit 'Update Shift,' I am met with the following error:

Started POST "/shifts" for 127.0.0.1 at 2011-08-21 20:03:10 -0700
ActionController::RoutingError (No route matches "/shifts"):

Obviously, this POST (or rather PUT) should be committing to 'shift/:id.'

Here's what I discovered: If I made my 'Edit' link non-remote, the proper HTML is created and the update works:

<form accept-charset="UTF-8" action="/tours/290" class="edit_tour" data-remote="true" id="entry_form" method="post">

BUT, when I make 'Edit' remote, the form looks like it's making a new object, even though the fields are properly populated and the button reads 'Update Shift':

<form accept-charset="UTF-8" action="/shifts" class="new_shift" data-remote="true" id="entry_form" method="post">

Here's my edit.js.rjs:

 page.replace_html('entry_form', render(:partial => 'shift_entry', :object => @schedule ))

And inside my controller:

def edit
  @shift = Shift.find(params[:id])
end

@schedule is set in my application controller. It appears my update.js.rjs isn't even being called. I'm using prototype.

Any ideas? I'm really stumped on this one. Thanks!

Edit:

A bit more info. Here's what my form header looks like when it's rendered with @shift = Shift.new (create):

<form accept-charset="UTF-8" action="/shifts" class="new_shift" data-remote="true" id="entry_form" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="KwY8w4l6qgKs6T3aMW8EUvl7V9I38HO8mz+QadBXKig=" /></div>

The header for the original (broken) edit is above. When I force :method => :put and try an 'Edit' -> 'Update', nothing changes (except my Create action breaks) and it looks like this:

<form accept-charset="UTF-8" action="/shifts" class="new_shift" data-remote="true" id="entry_form" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="KwY8w4l6qgKs6T3aMW8EUvl7V9I38HO8mz+QadBXKig=" /></div>


you need to add :method => :put inside of your :html options in your form_for line.

Should look like this:

<%= form_for @shift, :remote => true, :html => { :id => 'entry_form', :method => :put } do |f| %>
0

精彩评论

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

关注公众号