I've got User has_one Shop. Rails is not validating when I tried
create_shop or build_shop, neither in the browser nor the rails console.

My code:

class Shop < ActiveRecord::Base
  attr_protected :user_id
  belongs_to :user
  validates_presence_of :name, :primary_address, :city, :country_code,
:currency
end

class ShopsController < ApplicationController
  before_filter :signed_in_user, except: [:index, :show]
  before_filter :correct_user, only: [:edit, :update, :currency,
:update_currency]

  def new
    @shop = Shop.new
  end

  def create
    @shop = current_user.build_shop(params[:shop])

    if @shop.save
      flash[:success] = "Successfully added a shop."
      redirect_to user_path(current_user)
    else
      render 'new'
    end
  end
...
end

Error log (when tried in browser):

Started POST "/shops" for 127.0.0.1 at 2012-08-17 04:01:22 +0800
Processing by ShopsController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"bA+KBkV1CQTyb3H8lH2dGyOl6YR+Lp2I9jQodxDjXlE=",
"shop"=>{"name"=>"", "primary_address"=>"", "secondary_address"=>"",
"city"=>"", "postal_code"=>"", "country_code"=>"", "phone"=>"",
"email"=>"", "website"=>"", "facebook"=>"", "twitter"=>"",
"opening_hours"=>"", "description"=>"", "latitude"=>"", "longitude"=>"",
"currency"=>"AED"}, "commit"=>"Add shop"}
User Load (0.4ms)  SELECT "users".* FROM "users" WHERE
"users"."remember_token" = 'YykvzJ8PCZ5RFeE_ZomLXg' LIMIT 1
Shop Load (0.4ms)  SELECT "shops".* FROM "shops" WHERE "shops"."user_id"
= 1 LIMIT 1
(0.1ms)  BEGIN
(0.1ms)  COMMIT
(0.1ms)  BEGIN
(0.1ms)  ROLLBACK

In rails console:

irb(main):001:0> alice = User.find(1)
irb(main):002:0> alice.build_shop(name: "Alice Shop")
  Shop Load (0.6ms)  SELECT "shops".* FROM "shops" WHERE
"shops"."user_id" = 1 LIMIT 1
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
=> #<Shop id: nil, user_id: 1, name: "Alice Shop", primary_address: nil,
secondary_address: nil, city: nil, postal_code: nil, state_code: nil,
country_code: nil, phone: nil, email: nil, website: nil, facebook: nil,
twitter: nil, opening_hours: nil, description: nil, latitude: nil,
longitude: nil, logo: nil, currency: nil, created_at: nil, updated_at:
nil>

The weird thing is it does validate in the edit form using
update_attributes. But not when a User creates a new Shop.

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to