2012-02-28 20 views
0

我有兩個Rails應用程序。我的目標是通過一個聯繫表單獲得一個Rails應用程序,通過ActiveResource將信息保存到第二個Rails應用程序。我可以在沒有驗證的情況下將資源保存得很好,但是一旦我添加了驗證,就無法將錯誤消息顯示在帶有窗體的Rails應用程序中。與數據庫的Rails應用程序有一個CustomerTicket模式設置爲這樣:我怎麼沒有通過ActiveResource收到驗證錯誤

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 

*注:康康舞加載,建設我的資源

Rails應用程序與表格有相應的模型設置,如下所示:

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

在控制檯中Rails應用程序具有的ActiveResource模型我運行以下命令:

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={}> 

所以它承認該記錄不會被保存,但是我不能訪問的錯誤,以及是否它的驗證。任何想法我失蹤?

+0

rails版本? – Fivell 2012-03-04 20:29:49

回答

0

我認爲json版本的Active Resource存在一個bug。我配置我的Rails應用程序使用XML而不是json,一切都很好。

相關問題