开发者

Rails respond_with working incorrectly with json

I am trying to update a record remotely (via ajax), and get a response which includes the serialized record, but I am continually getting xhr.responseText = {} . I would like to receive a serialized version of the updated record.

Here is my view form...

=semantic_form_for theme, :html => {'data-type' => 'json', :id => :theme_display_type_form, :remote => true} do |form|
    -form.inputs do
        =form.input :display_type

The controller which processes...

respond_to :html, :json

... other actions...

def update
   flash[:success] = "Theme was successfully updated" if theme.update_attributes params[:theme]
   respond_with(theme)
end

and then the response is caught by the rails.js ajax:success event

$('#theme_display_type_form').bind('ajax:success', function(evt, data, status, xhr){
   var responseObject = $.parseJSON(xhr.responseText);
   alert(xhr.responseText); // = {}
   alert(responseObject); // = null
});

I must be missing something. Can anyone inform me of what I'm doing wrong?


EDIT

It looks like I may be having an issue with 'respond_with' as using the old respond_to method in my controller works properly...

def update
   flash[:success] = "Theme was success开发者_如何转开发fully updated" if theme.update_attributes params[:theme]
   respond_to do |format|
      format.json { render :json => theme }
   end
end

Anyone have any ideas why respond_with not working properly?


ANSWER

as per the lighthouse posting on https://rails.lighthouseapp.com/projects/8994/tickets/5199-respond_with-returns-on-put-and-delete-verb

it is normal that an UPDATE will return no object, since you already have the object in your possession..


I do have problems with respond_with but respond_to works correctly.

I see it in POST (create). It seems like respond_with works sometimes...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜