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

Undefined method `content' for Status

I do not understand this coding error and hope you can help me out. I was following your guides on devise when this error popped on statuses#new.

The Page:

NoMethodError in Statuses#new

Showing /var/lib/stickshift/51c8b66b5973cac7c2000059/app-root/data/456597/app/views/statuses/_form.html.erb where line #19 raised:

undefined method `content' for #<Status id: nil, created_at: nil, updated_at: nil, user_id: nil> Extracted source (around line #19):

16: </div> 17: 18: <%= f.input :user_id, collections: User.all, label_method: :full_name %> 19: <%= f.input :content %> 20: <div class="form-actions"> 21: <%= f.button :submit %> 22: </div> Trace of template inclusion: app/views/statuses/new.html.erb

Rails.root: /var/lib/stickshift/51c8b66b5973cac7c2000059/app-root/data/456597

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

Parameters:

None Show session dump

Show env dump

Response

Headers:

None

I don't understand why this is happening and can't narrow down any files for example. If you have any ideas on how this happened or how to fix it please help me out.

8 Answers

hmm sounds like a problem with your status model.

Please post the file (in full) from

app/models/status.rb

Thanks!

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

Hmm I was thinking maybe you didnt have content listing as an accessible attr in your model for some reason. Ok, can you post the full contents of.

app/views/status/_form.html.erb

app/controllers/statuses_controller.rb

and a link to video where you are getting your error

Yea that's what I was thinking. The videos talked about the models with undefined method fixing / masking the problem. I even tried to define a few methods via online tutorials.

app/models/status.rb

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

Doesn't seem to register my enter spaces :(

Error Page

NoMethodError in Statuses#new

Showing /var/lib/stickshift/51c8b66b5973cac7c2000059/app-root/data/456597/app/views/statuses/_form.html.erb where line #19 raised:

undefined method `content' for #<Status id: nil, created_at: nil, updated_at: nil, user_id: nil>
Extracted source (around line #19):

16:   </div>
17: 
18:    <%= f.input :user_id, collections: User.all, label_method: :full_name %>
19:    <%= f.input :content %>
20:   <div class="form-actions">
21:     <%= f.button :submit %>
22:   </div>
Trace of template inclusion: app/views/statuses/new.html.erb

Rails.root: /var/lib/stickshift/51c8b66b5973cac7c2000059/app-root/data/456597

Application Trace | Framework Trace | Full Trace
app/views/statuses/_form.html.erb:19:in `block in _app_views_statuses__form_html_erb___702039399460937707_70219092901200'
app/views/statuses/_form.html.erb:1:in `_app_views_statuses__form_html_erb___702039399460937707_70219092901200'
app/views/statuses/new.html.erb:3:in `_app_views_statuses_new_html_erb___2052960464619839141_70219092280560'
app/controllers/statuses_controller.rb:29:in `new'
Request

Parameters:

None
Show session dump

Show env dump

Response

Headers:

None

app/models/status.rb

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

app/views/status/_form.html.erb

class StatusesController < ApplicationController
  # GET /statuses
  # GET /statuses.json
  def index
    @statuses = Status.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @statuses }
    end
  end

  # GET /statuses/1
  # GET /statuses/1.json
  def show
    @status = Status.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @status }
    end
  end

  # GET /statuses/new
  # GET /statuses/new.json
  def new
    @status = Status.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @status }
    end
  end

  # GET /statuses/1/edit
  def edit
    @status = Status.find(params[:id])
  end

  # POST /statuses
  # POST /statuses.json
  def create
    @status = Status.new(params[:status])

    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Status was successfully created.' }
        format.json { render json: @status, status: :created, location: @status }
      else
        format.html { render action: "new" }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /statuses/1
  # PUT /statuses/1.json
  def update
    @status = Status.find(params[:id])

    respond_to do |format|
      if @status.update_attributes(params[:status])
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /statuses/1
  # DELETE /statuses/1.json
  def destroy
    @status = Status.find(params[:id])
    @status.destroy

    respond_to do |format|
      format.html { redirect_to statuses_url }
      format.json { head :no_content }
    end
  end
end

The problems started when I followed the steps of Installing Devise, I wasn't sure what was wrong so I continued in the tutorial but the problem didn't resolve itself. When I hit the testing section I couldn't get the tests to pass because of this error.

Apparently <%= f.input :content %> is invalid because it hasn't been defined. The problem is I don't know how to fix this, I am just a rookie.

I am running my ruby application in Cloud9ide like one of the videos advised.

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Could this have anything to do with my statuses problem since I am not running the server on a localhost? I am running it on a set ip and port. I am thinking it is a URL or routes problem.

config/routes.rb

MGN::Application.routes.draw do
  devise_for :users

  resources :statuses
  root to: 'statuses#index'


  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => 'welcome#index'

  # See how all your routes lay out with "rake routes"

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id))(.:format)'
end

No that should not be related. For a sanity check you can remove

<%= f.input :content %>

and try loading the page. Were you able to post your full _for.html.erb file? I think you might have posted the statuses controller instead.

Also, just a shot in the dark did you migrate the database after creating the status model?

I migrated, reset, migrated, rollback, migrated. Infact it wouldnt let me migrate until I went into sqlite development and dropped the status table "i've noticed". When I remove <%= f.input :content %> I am able to post a status without content but when I go to the all statuses page I get the same error. No method for content because the content is then attempting to display on that page. Without <%= f.input :content %> there is no content therefore the social network is useless. :(

I even re-scaffold the statuses a few times and that didn't even help.

hmm the only other thing I can think of is that you're not referencing the @status instance variable correctly in you _form.html.erb and index.html.erb views.

Does the top of your _form.html.erb look this this? Still need you to post this file in it's entirety :)

<%= 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 listing from being saved:</h2>
      <ul>
      <% @status.errors.full_messages.each do |msg| %>

....this is where you error begins...

The thing to look out for here is that your @status variable is in the correct place after form_for and that the |f| is correctly formatted. If this doesn't do the trick I'm afraid I've misunderstood the problem.

When you are stuck go to the beginning :) Smart.

app/views/statuses/_form.html.erb

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

  <div class="field">

  </div>

   <%= f.input :user_id, collections: User.all, label_method: :full_name %>
   <%= f.input :content %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

The problems with statuses started when I installed devise, edited the routes.rb, etc.

I was surfing the internet trying to fix the problem and source suggest it's either my app/models/user.rb or my app/controllers/statuses_controller.rb, personally I have no idea whats going on so I'm running these things by you first.

undefined method 'email'

Form_for undefined method path

rails error— undefined method

Why do I get undefined method

Attribute in Rails model appears to be nil when it's not

Ruby on Rails Nested resources undefined method path

A form mentioned different formatting for devise and migration databases