简体   繁体   中英

How to track down undefined method in rails

I'm getting this error, "NoMethodError: undefined method `user' for #" and not sure how to track down how my 'user' is not defined.

My user.rb is this:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

My property.rb is this:

class Property < ActiveRecord::Base
end

And my migration file is this:

class AddUserIdToProperties < ActiveRecord::Migration
  def change
    add_column :properties, :user_id, :interger
    add_index :properties, :user_id
  end
end

And here's my controller

 def create
    @property = Property.new(property_params)

      if @property.save
        redirect_to @property, notice: 'Property was successfully created.'
      else
        render :new
      end
    end

It seems to be breaking when in my properties_controller.rb I change this:

 def new
    @property = Property.new
  end

to this:

def new
    @property = current_user.property.build
  end

Here's my error logs

Started GET "/properties/new" for ::1 at 2015-04-27 00:45:49 -0400
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by PropertiesController#new as HTML
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 21ms (ActiveRecord: 0.5ms)

NoMethodError (undefined method `property' for #<User:0x007f9fa6f3e1c8>):
  app/controllers/properties_controller.rb:12:in `new'


  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.3ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (6.5ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (50.8ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.3ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (33.6ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.6ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.2ms)
  Rendered /Users/zak/.rvm/gems/ruby-2.2.1/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (7

By looking your migration its look like you have to add this,

class Property < ActiveRecord::Base
   belongs_to :user
end

class User < ActiveRecord::Base
   has_one :property
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM