Jeff
Jeff

Reputation: 4413

Get response from respond_with request rails

Given this user action:

class UsersController < ApplicationController
    respond_to :html, :js  

    def update
        @user = User.find(params[:id])
        # do stuff...
        respond_with(@user)
    end
    #other functions
end

How do I print the response in my app/views/users/update.js.erb file?

I can access the @user variable in there, but how do I get the "status" or whether or not it was successful?

My current app/views/users/update.js.erb file looks like this:

console.log("update?");
console.log("<%=@user %>");

which prints:

udate?
#&lt;User:0x639a470&gt;

in the console.

Basically, I want to know if it was successful before trying to do things with @user.

Upvotes: 1

Views: 109

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34318

Use:

console.log("<%[email protected]_json%>");

Update

def update
  @user = User.find(params[:id])
  # do stuff...

  if @user.save
    # user update was successful
    respond_with(@user)
  else
    flash[:error] = "User Update Failed"
    render :action => "some_action"
  end
end

Upvotes: 1

Related Questions