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
Ian Warner
5,563 PointsBuild simple Ruby Application project "treebook" NoMethodError in Statuses#new error in "Customizing forms/ creating relationships"
After adding the following line
<%= f.input :user_id %>
to my form.html.erb file, as instructed in the video "creating relationships"
I get the following error:
NoMethodError in Statuses#new
undefined method `user_id' for #<Status:0x00000102eb6d30>
This is what my form.html.erb file looks like:
<%= simple_form_for(@status, html: {class: "form-horizontal"}) do |f| %>
<% if @status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@status.errors.count, "error") %> prohibited this status from being saved:</h2>
<ul>
<% @status.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :user_id %>
<%= f.input :content %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
If someone could help me out I'd really appreciate it.
Thanks again!
4 Answers
Stone Preston
42,016 Pointsmake sure you have a migration in your db/migrate directory than includes a call to the add_index method and is passed the :user_id symbol
then run
rake db:migrate
Your database probably doesnt contain the user id field so you need to run your migrations that add that field.
Ian Warner
5,563 PointsHey Stone, Thanks for your reply but I believe my db migrate file is up to date. This is what my db/migrate/add_user_id_to_statuses.rb file looks like.
class AddUserIdToStatuses < ActiveRecord::Migration
def change
add_column :statuses, :user_id, :integer
add_index :statuses, :user_id
remove_column :statuses, :name
end
end
I also ran rake db:migrate and nothing yet. Any other ideas? Thanks for your help.
Paul Dariye
Courses Plus Student 9,097 PointsI did what Stone Preston recommended and it worked. You should check your migration again
add_index :statuses, :user_id
Make sure you're in the project directory when you run the command
rake db:migrate
then run your rails server. this should work. It did work for me. I was so sure I did the same thing before he even sent the hint. I gave my machine a hiatus before running it again. lol. all the best bud!
Ian Warner
5,563 PointsHey Paul, I tried what Stone recommended but I'm not sure which migration file to add the following code to:
add_index: statuses, :user_id
I tried adding the above line to the different migration files but and then running the rake db:migrate command and then running the rails server, no luck yet.
It would be nice to get past this section in the project so I can move forward. :)
Thanks!
Paul Dariye
Courses Plus Student 9,097 Pointshey man, go to
- db folder under app
- go to the migrate folder under db
- look for a file that has a suffix of add_user_id_to_statuses
you can add your code there. Here's my code from there(am sure it's same as yours)
class AddUserIdToStatuses < ActiveRecord::Migration
def change
add_column :statuses, :user_id, :integer
add_index :statuses, :user_id
remove_column :statuses, :name
end
end
Hope this helps.
Ian Warner
5,563 PointsHey Paul, Thanks for your help, but I checked that file and it was the same as yours. I even tried the rake db:migrate again and then ran the rails server no luck! :( There must be some step I missed somewhere. It's not refreshing the page and allowing me to add "a new status" like Jim was able to do in the video. Any other suggestions that you had to change to get it to work would be much appreciated. Thanks man!
Paul Dariye
Courses Plus Student 9,097 PointsSorry about that. Try pulling my repo from github and then migrate the db to update your db. http://github.com/pauldd91/treebook
I hope this helps. Let me know if it does.
Ian Warner
5,563 PointsHey Paul, I looked at repo online but I don't think I can pull it unless you give me an invite request. My name on github.com is crazycanuck84 if you want to make an invite request to clone your repo. Thanks for your help. I really appreciate it.
Thanks, Ian
Paul Dariye
Courses Plus Student 9,097 Pointssorry for the late reply, I did not see this earlier. I will add you.
Ian Warner
5,563 PointsHey Paul, No worries I started going through the project from scratch again to see where I went wrong, but this would help for sure. I really appreciate your help.
Thanks, Ian
Paul Dariye
Courses Plus Student 9,097 PointsAnytime!
Paul Dariye
Courses Plus Student 9,097 PointsThank you Stone
Julian McNeill
9,445 Pointswhat is the code to update the db
Paul Dariye
Courses Plus Student 9,097 Pointsrake db:migrate
Jesse Waites
146 PointsI am having the same exact issue. My migrations are find, rake has ran, server restarted. Please advise. Im using Rails 4 btw. I think there are Rails 4 issues at work here.
Paul Dariye
Courses Plus Student 9,097 PointsPaul Dariye
Courses Plus Student 9,097 PointsI have this same problem. I haven't figured it out yet. Thought I'd take the time out to go over Why's poignant guide to ruby.