0

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
5
  • Are you using your 'user' in one of your views? For example: <%= property.user_id %> Commented Apr 27, 2015 at 4:39
  • And the meesage that s giving your that error? Have a look in your logs... you will find the error that you list above (somewhere), and about 30 lines of scrolling text (all filenames with numbers after them) after that... it's called a stacktrace. copy/paste the first 5-10 lines of that and edit and add it to your question (do not copy it into comments as formatting is really crap here). Commented Apr 27, 2015 at 4:39
  • Maybe your problem (if your migration ran successfully) is that you have not specified What the associations are between the User and Property classes. In this case a Property belongs_to :user. Don't forget to add has_one/has_many on the User model. Commented Apr 27, 2015 at 4:43
  • I was typing the above comment on my phone while the edit was being made. After reading the edit,the above is your problem. I'm on a phone and cannot give proper answer. Commented Apr 27, 2015 at 4:45
  • Are you trying to do something like: @property = Property.new({:user_id => @property.user_id}) ? Let me know if this works. Commented Apr 27, 2015 at 4:56

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Because the Property class has a user_id column, it should be: belongs_to :user. (The has_one or has_many goes in the User class).
Don't forget to change :property to :user :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.