Rails doesn't validate create_model or build_model (has_one association)

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:

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

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.

Those methods create the object but do not save it (see section 4.1.1.3 in the Rails Guide on Associations). Validation happens when the record is saved (or valid? is called).

Colin

Colin Law wrote in post #1072628:

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.

Those methods create the object but do not save it (see section 4.1.1.3 in the Rails Guide on Associations). Validation happens when the record is saved (or valid? is called).

Colin

Yeah but in the browser, it just went straight to the show action without showing any validation warning at all. If it fails validation, it should render the shops#new action again.

Here's the error message it gives me when I click submit in the shops#new form without inputting anything and then it tried to go to the show action:

Routing Error

No route matches {:action=>"show", :controller=>"shops", :id=>#<Shop id: nil, user_id: 1, name: "", primary_address: "", secondary_address: "", city: "", postal_code: "", state_code: nil, country_code: "", phone: "", email: "", website: "", facebook: "", twitter: "", opening_hours: "", description: "", latitude: nil, longitude: nil, logo: nil, currency: "AED", created_at: nil, updated_at: nil>}

Colin Law wrote in post #1072628:

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.

Those methods create the object but do not save it (see section 4.1.1.3 in the Rails Guide on Associations). Validation happens when the record is saved (or valid? is called).

Colin

Yeah but in the browser, it just went straight to the show action without showing any validation warning at all. If it fails validation, it should render the shops#new action again.

Well you have a bug in your code then. Have a look in development.log to see what happens when you click the link. Then have a look at the Rails Guide on Debugging to get further ideas on how to debug the code.