rubyonrailsin

A Ruby and Rails talk

Sunday, March 21, 2010


Re: [Rails] Rails 3: Update responding with empty json

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

On Sun, Mar 21, 2010 at 9:44 AM, Fabio Kreusch <fabiokr@gmail.com> wrote:

Hi all!

I'm playing with Rails 3, and on one of my controllers I have the
following update method defined:

def update
   @category = @user.categories.find(params[:id])
   @category.update_attributes(params[:ecategory])
   @category.save
   respond_with @category
 end


Fabio, you should not be calling save within the update
action because 'update_attributes' calls save implicitly.
Thus, you should do something like the following:

class CategoriesController < ApplicationController::Base

  respond_to :html, :json

def update
   @category = @user.categories.find( params[:id] )
      if @category.update_attributes( params[:category] )
     flash[:notice] = 'Category was successfully created.'
    end
   respond_with( @category )
  end
end
Good luck,
-Conrad
I'm using Jquery to interact with the controllers, and when I do an
ajax PUT, the update action is correctly executed (the fields are
updated), but the action returns an empty JSON object, which causes
the Jquery ajax function to fall into an error.

I'm I doing something wrong?

Thank you all,
Fabio.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive