Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Lut Garcia
3,631 PointsRoR NoMethodError in Credits#new [SOLVED]
Hi, I am doing my homework around the building a simple RoR app and got stuck with the following:
I created a Scaffold called "Credit", and a relationship between the User and Credit model:
<strong>Here are my files</strong>
credit.rb
class Credit < ActiveRecord::Base
attr_accessible :amount, :user_id
belongs_to :user
belongs_to :merchant
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :profile_name
# attr_accessible :title, :body
has_many :statuses
has_many :merchants
has_many :credits
def full_name
first_name + " " + last_name
end
end
<strong>When trying to create a new credit entry with the following form:</strong>
<%= simple_form_for(@credit) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :amount %>
<%= f.input :user_id, collection: User.all %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
<strong>I get the following error, </strong>
NoMethodError in Credits#new
Showing/Users/andreucasadella/rails_projects/monedero/app/view credits/_form.html.erb where line #6 raised:
undefined method `user_id' for #<Credit id: nil, amount: nil, created_at: nil, updated_at: nil>
<strong>Any idea?</strong>
1 Answer
Lut Garcia
3,631 PointsSolution:
I was missing a db migration file. Now it works.
class AddUserIdtoCredit < ActiveRecord::Migration
def change
add_column :credits, :user_id, :integer
end
end –