Search code examples
ruby-on-railsjsonruby-on-rails-3.1respond-torespond-with

How to convert this respond_to options to use Rails 3 version?


respond_to do |format|
  if @user.save
    format.js { render :nothing => true, :status => :ok, :location => @user }
  else
    format.js { render :json => @user.errors, :status => :unprocessable_entity }
  end
end

All options I've tried (like putting respond_to :js at the top of controller, etc) don't quite work the way as in this.


Solution

  • Rails 3 Format:

    Use respond_to :json and respond_with(@user)

      respond_to :json  # You can also add  , :html, :xml  etc.
    
      def create
        @user= User.new(params[:user])
          #---For html flash
          #if @user.save
          #  flash[:notice] = "Successfully created user."
          #end
        respond_with(@user)
      end
    
    # Also, add :remote => :true, :format => :json to the form.