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
Trevor Williams
5,859 PointsRuby on Rails: Creating Relationships problem
Hi, I'm stuck on the "Creating Relationships" in the "Customizing Forms" lesson. I can't figure out whats going wrong with my code, (entering user form: simple_form)
When I add this code:
<%= f.input :user_id %>
to my 20130211233331_create_statuses.rb file, I get this error...
"NoMethodError in Statuses#new
Showing /Users/trevorwilliams/projects/Treebook/app/views/statuses/_form.html.erb where line #13 raised:
undefined method `user_id' for #<Status:0x007fb68fa16ba0> Extracted source (around line #13):
10: </ul> 11: </div> 12: <% end %> 13: <%= f.input :user_id %> 14: <%= f.input :content %> 15: <div class="form-actions"> 16: <%= f.button :submit %> Trace of template inclusion: app/views/statuses/new.html.erb
Rails.root: /Users/trevorwilliams/projects/Treebook"
So every time I try to create a new status update, I get the "NoMethodError in Statuses#new" error. When I remove <%= f.input :user_id %> from the file, I can then create a new status...
6 Answers
Alexander Batalov
21,887 PointsI noticed that for some reasons it was like rake db:migrate not working properly... After some research I did the following:
Downloaded project files from treehouse to check if I have some typos in my code. And there was big diff in db/schema.rb file.
Copied all the data from treehouse's project and paste it in my db/schema.rb file.
-
Since you can not modify this file (file is auto-generated from the current state of the database.) I ran next command in terminal
rake db:schema:load
And it did the trick :) Hope it helps
Trevor Williams
5,859 PointsI fixed it! I was following the videos step by step, so when Jim's new status page worked and mine didn't, I tripped out! After going back and checking my code for errors, I decided to watch the video again and see if I could pick something up. After watching the next step in the video, I decided I was going to try the next step since it had :user_id, and it worked! Just wondering, did this happen because I have a newer version of bootstrap?
I added ( :user_id ) to,
status.rb
class Status < ActiveRecord::Base attr_accessible :content, :user_id end
Adam Waxman
8,779 PointsI'm having the same problem.
I've tried to run rake db:migrate again but still not working. Any suggestions?
Adam Waxman
8,779 PointsAlso, this is what my db migration add_user_id_to_statuses file looks like if that helps:
class AddUserIdToStatuses < ActiveRecord::Migration
def change
add_column :statuses, :user_id, :integer
add_index :statuses, :user_id
remove_column :statuses, :name
end
end
Rodrigo Hooli Coolio Esquire
5,309 PointsHi Adam,
This error kinda ruined my sunday afternoon. Here's what I figured out for my similar error. Hope this helps.
Re: my entry
According to a response from the instructor Jim, on another user's entry:
"NoMethodError in the case of models is usually a symptom of the database not having the right columns. Since you didn't have the method :user_id, and it was a column we were adding at that point in time, I would guess that you may have forgot to migrate the database, or the migration may have failed the first time around."
I also ran rake db:migrate again and it did not solve the error. I have already gone through the Michael Hartl RoR tutorial and we had used SQLite Database Browser. sqlitebrowser.sourceforge.net/ I'm using 2.0 but some have to use 1.1 if you run into trouble. In taking a look at the database information I wanted to see if all of my columns were there. [I apologize but apparently you can't paste images into this forum.]
After opening Treebook's development.sqlite3 file with the browser software I saw the right columns but in clicking into the ["Browse Data" tab : statuses table drop down] I could see that the user_id cell was 0. I figured this was the nil that was causing the error so I manually changed it in SQLite Database Browser to 4 to match the id cell count.
Then I saved and went right to check the site and I could get back to all the pages that were crashing before making the change. There's probably a way to fix this through the Rails Console but if you're receiving nil errors this could be why. Also SQLite Database Browser is a tool for visual checks of what's sitting in the dev database.
Jim Hoskins
Treehouse Guest TeacherNoMethodError in the case of models is usually a symptom of the database not having the right columns. Since you didn't have the method :user_id, and it was a column we were adding at that point in time, I would guess that you may have forgot to migrate the database, or the migration may have failed the first time around.
Maurice Womack
822 PointsHow and why would a migration fail? I have tried to add the user_id migration but it apparently is not working. When I check my schema.rb, the user_id column is not showing. Any suggestions?