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

NoMethodError in StatusesController#create

I don't know what happened, but I screwed some stuff up along the way. I've been trying to debug so I can move on to making the profile pages but I am stuck. I think maybe I screwed something up because I tried erasing my database because I thought that was the problem. Anyway, here is the error message I get when I try to post a new status:

NoMethodError in StatusesController#create undefined method `content' for #<Status:0x00000102ed1b08>

app/controllers/statuses_controller.rb:47:in `block in create'
app/controllers/statuses_controller.rb:46:in `create'

Request

Parameters:

{"utf8"=>"?",
"authenticity_token"=>"v3ZC7xMy8tn7GqF/NekIyP+fq5WTPrj2vYyfmHXVXGE=",
"status"=>{"user_id"=>"",
"context"=>"this is a test"},
"commit"=>"Create Status"}

Response

Headers:

None

here are lines 46 and 47 from statuses_controller.rb

respond_to do |format|
if @status.save

Help! Please! :)

2 Answers

Can You post your entire statuses controller and status model?

statuses_controller.rb

class StatusesController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update]
  # 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

status.rb

class Status < ActiveRecord::Base
  attr_accessible :context, :user_id
  belongs_to :user

  validates :content, presence: true,
                      length: { minimum: 2 }
  validates :user_id, presence: true
end

Look like you might have the attribute defined as 'context' instead of 'content' look at the file in db/schema.rb and look under the status table to see if you originally defined it as 'content' or 'context'. The status can't be created because rails is getting "confused" about this. Or just look at how it is defined in your forms.

My guess is if you switch

validates :content

to

validates :context

rails would allow the status to be created. Just as along as you have consistent naming (whatever it may be) you should be alright :) Let me know if that doesn't work by posting your db/schema/rb file or if you have any other questions.

-twiz

woohoo! thank you! I remember there was some confusion when I was writing that part, something didn't work for me and I saw here on the forum someone fixed it by writing context instead of content or the other way around and I must have forgotten the next time that came up. All good now!

Are you still able to open a rails server? If yes then just start over from where you messed up (not saying restart the whole project)

I haven't been saving as I go, I don't know where I messed it up. Lesson learned :(