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
Kerel Lacy
8,474 PointsNoMethodError in Statuses#new
I'm not sure how to fix this. It's telling me there is an undefined method "name"
undefined method `name' for #<Status:0x007f9f030caa90>
13 Answers
If you post the actual code from which this error is originating, I'm sure more people would be able to help!
Kerel Lacy
8,474 PointsThis is code
NoMethodError in Statuses#index
Showing /Users/KerelLacy/Desktop/Projects/treebook/app/views/statuses/index.html.erb where line #9 raised:
undefined method `user_id' for nil:NilClass Extracted source (around line #9):
6: <% @statuses.each do |status| %> 7: <div class="status"> 8: 9: <strong><%= @status.user_id %></strong> 10: <p> <%= status.content %></p> 11: <div class="meta"> 12: <%= link_to time_ago_in_words(status.created_at) + " ago", status %> Rails.root: /Users/KerelLacy/Desktop/Projects/treebook
Application Trace | Framework Trace | Full Trace
app/views/statuses/index.html.erb:9:in block in _app_views_statuses_index_html_erb__1384172865432370789_70346896282000'
app/views/statuses/index.html.erb:6:ineach'
app/views/statuses/index.html.erb:6:in _app_views_statuses_index_html_erb__1384172865432370789_70346896282000'
app/controllers/statuses_controller.rb:7:inindex'
By typing @status.user_id - you're calling the "method" 'user_id' on the 'status'. The error is telling you that there is no method called 'user_id'.
The 'user_id' method would look like this (with your code for the method in the middle).
def user_id
end
It would be more useful if we could see all of your code - so we can understand what you're trying to achieve :) Maybe post your /controllers/statuses_controller.rb file?
Kerel Lacy
8,474 PointsIn what file would I add this method? I also get an error when I try to create/post a new status.
Showing /Users/KerelLacy/Desktop/Projects/treebook/app/views/statuses/_form.html.erb where line #16 raised:
undefined method `name' for #<Status:0x007fc66b4fe5a0> Extracted source (around line #16):
13: 14: <div class="field"> 15: <%= f.label :name %><br /> 16: <%= f.text_field :name %> 17: </div> 18: <div class="field"> 19: <%= f.label :content %><br /> Trace of template inclusion: app/views/statuses/new.html.erb
Rails.root: /Users/KerelLacy/Desktop/Projects/treebook
Application Trace | Framework Trace | Full Trace app/views/statuses/form.html.erb:16:in `block in _app_views_statusesform_html_erb2101612916463847386_70245097028060' app/views/statuses/_form.html.erb:1:in `_app_views_statusesform_html_erb2101612916463847386_70245097028060' app/views/statuses/new.html.erb:3:in `_app_views_statuses_new_html_erb__2926593961717318689_70245096965220' app/controllers/statuses_controller.rb:30:in `new'
It should probably go into the controller you've created for statuses.
For the second error, have you included an attr_accessor for name (which would create the methods you need) in your model?
The following = copied/pasted from http://guides.rubyonrails.org/getting_started.html
2.1.1 Models
A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, each table in your database will correspond to one model in your application. The bulk of your application’s business logic will be concentrated in the models.
2.1.2 Views
Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that perform tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.
2.1.3 Controllers
Controllers provide the “glue” between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.
Kerel Lacy
8,474 PointsYes, this is the code from my status.rb
class Status < ActiveRecord::Base attr_accessible :content, :name, :user_id end
taken from another thread
Have you run your db migrations?
rake db:migrate
Kerel Lacy
8,474 PointsYes, I have and when I run rake db:migrate my command prompt doesn't return anything.
What I've decided to do is start completely over and see if there are any steps I have missed.
Maybe it'd be better if someone with more knowledge was helping you out :)
But, you can check to see if the tables were created in your database. Are you using Sqlite or MySQL?
Kerel Lacy
8,474 PointsI'm using Sqlite
Kerel Lacy
8,474 PointsOk yeah my app broke again, right after I do the Authentication System lessons it breaks I assume theres something with my database or something that isn't working properly.
Kerel Lacy
8,474 PointsAha so I've figured out. If you continue watching the next set of videos regarding forums Jim Shows you how to fixed the issues.
Mario Sanchez
3,037 Pointsi'm having the same problem, what set of videos are you referring to exactly?
ah ok, good to hear :) happy new year Kerel!