I have two Rails applications. My goal is to get one Rails application with a contact form to save information to the second Rails application via ActiveResource. I can get the resources to save just fine without validation, but once I add the validation in I can't get the error message to appear in the Rails app with the form. The Rails app with the database has a CustomerTicket model setup as so:
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
And the controller:
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
*Note: CanCan is loading and building my resources
The Rails app with the form has a corresponding model setup like so:
class CustomerTicket < ActiveResource::Base
self.site = "http://myurl:3000/"
end
In the console of the Rails app that has the ActiveResource model I run the following:
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={}>
So it recognizes that the record isn't saved, however I can't gain access to errors and whether or not its validated. Any idea what I'm missing?
I think there's a bug in the json version of Active Resource. I configured my Rails app to use XML instead of json and all is well.