为什么我没有通过ActiveResource收到验证错误

时间:2012-02-28 21:41:12

标签: ruby-on-rails-3 activeresource

我有两个Rails应用程序。我的目标是使用联系表单获取一个Rails应用程序,以通过ActiveResource将信息保存到第二个Rails应用程序。我可以在没有验证的情况下获得保存的资源,但是一旦我添加了验证,我就无法将错误消息显示在带有表单的Rails应用程序中。带有数据库的Rails应用程序具有CustomerTicket模型设置,如下所示:

class CustomerTicket < ActiveRecord::Base
  validates_presence_of :first_name, :last_name, :email, :phone, :address1, :address2,
                        :city, :state, :postcode, :question

  validates_length_of :phone, { :minimum => 7}

  validates_format_of :email, { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => 'Please include a proper e-mail address.' }
end

控制器:

class CustomerTicketsController < ApplicationController
  load_and_authorize_resource
  skip_authorization_check :only => [:new, :create]
  skip_authorize_resource :only => [:new, :create]
  def index
    @q = CustomerTicket.search(params[:q])
    @customer_tickets = @q.result.order('created_at DESC').page params[:page]

    respond_to do |format|
      format.html # index.html.erb
    end
  end

  def show
    @customer_ticket = CustomerTicket.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
    end
  end

  def new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @customer_ticket }
    end
  end

  # GET /customer_tickets/1/edit
  def edit
  end

  # POST /customer_tickets
  # POST /customer_tickets.json
  def create
    respond_to do |format|
      if @customer_ticket.save
        format.html { redirect_to @customer_ticket, notice: 'Customer ticket was successfully created.' }
        format.json { render json: @customer_ticket, status: :created, location: @customer_ticket }
      else
        format.html { render action: "new" }
        format.json { render json: @customer_ticket.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @customer_ticket.update_attributes(params[:customer_ticket])
        format.html { redirect_to @customer_ticket, notice: 'Customer ticket was successfully updated.' }
      else
        format.html { render action: "edit" }
      end
    end
  end

  def destroy
    @customer_ticket.destroy

    respond_to do |format|
      format.html { redirect_to customer_tickets_url }
    end
  end
end

*注意:CanCan正在加载和构建我的资源

带有表单的Rails应用程序具有相应的模型设置,如下所示:

class CustomerTicket < ActiveResource::Base
  self.site = "http://myurl:3000/"
end

在具有ActiveResource模型的Rails应用程序的控制台中,我运行以下命令:

ruby-1.9.2-p290 :001 > c = CustomerTicket.new(:first_name => "Josh")
 => #<CustomerTicket:0x007f7f89815528 @attributes={"first_name"=>"Josh"}, @prefix_options={}, @persisted=false>

ruby-1.9.2-p290 :002 > c.save
 => false

ruby-1.9.2-p290 :003 > c.valid?
 => true

ruby-1.9.2-p290 :004 > c.errors.count
 => 0

ruby-1.9.2-p290 :005 > c.errors
 => #<ActiveResource::Errors:0x007f7f88b165c0 @base=#<CustomerTicket:0x007f7f89815528 @attributes={"first_name"=>"Josh"}, @prefix_options={}, @persisted=false, @remote_errors=#<ActiveResource::ResourceInvalid: Failed.  Response code = 422.  Response message = Unprocessable Entity.>, @validation_context=nil, @errors=#<ActiveResource::Errors:0x007f7f88b165c0 ...>>, @messages={}>

因此它识别出记录未保存,但是我无法访问错误以及是否验证了错误。知道我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我认为json版本的Active Resource存在一个错误。我将我的Rails应用程序配置为使用XML而不是json,一切都很好。

相关问题