开发者

ActiveResource error handling

开发者 https://www.devze.com 2023-01-04 03:47 出处:网络
I have been searching for a while and yet I am not able to find a satisfactory answer as yet. I have two apps. FrontApp and BackApp. FrontApp has an active-resource which mimics a model in BackApp. Al

I have been searching for a while and yet I am not able to find a satisfactory answer as yet. I have two apps. FrontApp and BackApp. FrontApp has an active-resource which mimics a model in BackApp. All the model level validations live in BackApp and I need to handle those BackApp validations in FrontApp.

I have following active-resource code:

class RemoteUser < ActiveResource::Base
  self.site = SITE
  self.format = :json
  self.element_name = "user"
end

This mimics a model which is as follows

class User < ActiveRecord::Base

  attr_accessor :username, :password

  validates_presence_of :username
  validates_presence_of :password
end

Whenever I create a new RemoteUser in front app; I call .save on it. for example:

user = RemoteSession.new(:username => "user", :password => "")
user.save

However, since the password is blank, I need to pass back the errors to FrontApp from BackApp. This is not happening. I just don't understand how to do that successfully. This must be a common in开发者_如何学Ctegration scenario; but there doesn't seem to be a good documentation for it?

My restful controller that acts as a proxy is as follows:

class UsersController < ActionController::Base
  def create
    respond_to do |format|
      format.json do
        user = User.new(:username => params[:username], :password => params[:password])
        if user.save
          render :json => user
        else
          render :json => user.errors, :status => :unprocessable_entity
        end
      end
    end
  end
end

What is it that I am missing? Any help will be much appreciated.

Cheers


From rails source code I figured out that the reason ActiveResource didn't get errors was because I wasn't assigning errors to "errors" tag in json. It's undocumented but required. :)

So my code should have been:

render :json => {:errors => user.errors}, :status => :unprocessable_entity


In the code:

class UsersController < ActionController::Base
  def create
    respond_to do |format|
      format.json do
        user = User.new(:username => params[:username], :password => params[:password])
        if user.save
          render :json => user
        else
          render :json => user.errors, :status => :unprocessable_entity
        end
      end
    end
  end
end

try to replace

user = User.new(:username => params[:username], :password => params[:password])

with

user = User.new(params[:user])

Your active-resource model pass the params like the hash above:

:user => { :username => "xpto", :password => "yst" }


This solution worked for me: https://stackoverflow.com/a/10051362/311744

update action:

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

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json {

          render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

Calling controller:

@remote_user = RemoteUser.find(params[:id])
if (@remote_user.update_attributes(params[:remote_user]))
  redirect_to([:admin, @remote_user], notice: 'Remote user was successfully updated.')
else
  flash[:error] = @remote_user.errors.full_messages
  render action: 'edit'
end
0

精彩评论

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