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

Ruby

Ruby on Rails Treebook — Creating Relationships error.

Hi there.

I've seen other threads on this, but I couldn't figure out what I was doing wrong.

I've completely followed the instructions that have been giving in the videos.

The problem comes when changing to <%= f.input :user_id %>, which resolves in the following error when going to: http://localhost:3000/statuses/new

NoMethodError in Statuses#new

Showing /Users/x/treebook/app/views/statuses/_form.html.erb where line #14 raised:

undefined method `user_id' for #<Status:0x007faea5310190> Extracted source (around line #14):

11: </div> 12: <% end %> 13: 14: <%= f.input :user_id %> 15: <%= f.input :content %> 16: <div class="form-actions"> 17: <%= f.button :submit %> Trace of template inclusion: app/views/statuses/new.html.erb

Rails.root: /Users/x/treebook

Application Trace | Framework Trace | Full Trace app/views/statuses/form.html.erb:14:in `block in _app_views_statusesform_html_erb437346370271433243_70194027079180' app/views/statuses/_form.html.erb:1:in `_app_views_statusesform_html_erb_437346370271433243_70194027079180' app/views/statuses/new.html.erb:3:in `_app_views_statuses_new_html_erb600432127178792967_70194003053640' app/controllers/statuses_controller.rb:29:in `new' Request

Parameters:

None Show session dump

Show env dump

Response

Headers:

None

My code on _form.html.erb is as follows:

<%= 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 %>

9 Answers

I really have no idea what I am doing, but this worked. I found on some forums that it could be a problem with my old data so I ran: rake db:migrate:reset

After I did that it worked.... not sure why.

Awesome! db:migrate:reset worked for me. Thanks a ton.

Hello,

In status.rb do you have:

class Status < ActiveRecord::Base attr_accessible :content, :user_id

when I add that I get attr_accessible is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add protected_attributes to your Gemfile to use old one.

when I add that I get attr_accessible is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add protected_attributes to your Gemfile to use old one.

No, I didn't.

I've just added it though, and my status.rb is as follows:

class Status < ActiveRecord::Base attr_accessible :content, :user_id end

but I still get the error:

NoMethodError in Statuses#new

Showing /Users/x/treebook/app/views/statuses/_form.html.erb where line #14 raised:

undefined method `user_id' for #<Status:0x007f9ea8c2c0d8> Extracted source (around line #14):

11: </div> 12: <% end %> 13: 14: <%= f.input :user_id %> 15: <%= f.input :content %> 16: <div class="form-actions"> 17: <%= f.button :submit %> Trace of template inclusion: app/views/statuses/new.html.erb

Rails.root: /Usersx/treebook

Application Trace | Framework Trace | Full Trace app/views/statuses/form.html.erb:14:in `block in _app_views_statusesform_html_erb1356767309347397406_70159733755400' app/views/statuses/_form.html.erb:1:in `_app_views_statusesform_html_erb1356767309347397406_70159733755400' app/views/statuses/new.html.erb:3:in `_app_views_statuses_new_html_erb_2715395134366097675_70159720316420' app/controllers/statuses_controller.rb:29:in `new' Request

Parameters:

None Show session dump

Show env dump

Response

Headers:

None

Cheers.

new.html.erb should just look like:

<h1>New status</h1>

<%= render 'form' %>

<%= link_to 'Back', statuses_path %>

your _form.html.erb should look 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, collection: User.all, label_method: :full_name %> <%= f.input :content %> <div class="form-actions"> <%= f.button :submit %> </div> <% end %>

Right, my new.html.erb is identical.

My _form.html.erb wasn't identical to what you just wrote; it only had:

<%= f.input :user_id %> (and not the rest attributes and methods)

I just added exactly what you wrote for _form.html.erb, and I'm still getting the following error:

NoMethodError in Statuses#new

Showing /Users/x/treebook/app/views/statuses/_form.html.erb where line #14 raised:

undefined method `user_id' for #<Status:0x007f9eaac0d438> Extracted source (around line #14):

11: </div> 12: <% end %> 13: 14: <%= f.input :user_id, collection: User.all, label_method: :full_name %> 15: <%= f.input :content %> 16: <%= f.button :submit %> 17: <% end %> Trace of template inclusion: app/views/statuses/new.html.erb

Rails.root: /Users/x/treebook

Application Trace | Framework Trace | Full Trace app/views/statuses/form.html.erb:14:in `block in _app_views_statusesform_html_erb1356767309347397406_70159704608780' app/views/statuses/_form.html.erb:1:in `_app_views_statusesform_html_erb1356767309347397406_70159704608780' app/views/statuses/new.html.erb:3:in `_app_views_statuses_new_html_erb_2715395134366097675_70159720316420' app/controllers/statuses_controller.rb:29:in `new' Request

Parameters:

None Show session dump

_csrf_token: "H8b37JQh1FZXDSeHxVZpANaUubkTdp28SeTPyrrPRTk=" session_id: "c3d644ccce301f2627e508ddd9ca0f77" warden.user.user.key: ["User", [3], "$2a$10$va.y5nrLthdxM/ABpcsw3."] Show env dump

GATEWAY_INTERFACE: "CGI/1.1" HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8" HTTP_ACCEPT_CHARSET: "ISO-8859-1,utf-8;q=0.7,*;q=0.3" HTTP_ACCEPT_ENCODING: "gzip,deflate,sdch" HTTP_ACCEPT_LANGUAGE: "da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4" HTTP_CACHE_CONTROL: "max-age=0" REMOTE_ADDR: "127.0.0.1" REMOTE_HOST: "localhost" SERVER_NAME: "localhost" SERVER_PROTOCOL: "HTTP/1.1" Response

Headers:

None

Magnus,

Here is the link to the treebook github: https://github.com/jasonseifer/treebook

You can browse through the files in question and see the commit's by either Jason, or Jim.

Once in the file, click history and it will show their commits so you are not looking at onces that reflect a future project.

I hope this helps. I use it for reference when I am stuck and make sure my code is in line. I actually install simple_form before I began this project in my Gemfile, so my code will look different.

Okay,

I'll check it out. Thanks a lot.

I'm having the same exact problem. @magnus, did you ever find a solution?

Same problem here. Please update if you figured out what's wrong!