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

Getting NoMethodError in Statuses#index

Just finished stage 2 of Simple Ruby on Rails Application and am getting a NoMethodError in Statuses#index when i call status.content. If i do rake db:reset and delete all my statuses then it works fine, but everytime i create a new status it gives this error. Here is my code: <div class="page-header"> <h1>All of the Statuses</h1> </div>

<%= link_to "Post a New Status", new_status_path, class: "btn btn-success" %> <% @statuses.each do |status| %> <div class="status"> <strong><%= status.name %></strong> <p><%= status.content %></p> <div class="meta"> <%= link_to time_ago_in_words(status.created_at) + " ago", status %> <span class="admin"> | <%= link_to "Edit", edit_status_path(status) %> | <%= link_to "Delete", status, method: :delete, data: {confirm: "Are you sure you want to delete this status"} %> </span> </div> </div> <% end %>

6 Answers

Glenn Harris
Glenn Harris
7,101 Points

I found the problem and got it to work.

Your column in the database is named "context", not "content". To fix this change the method on your index page to <%= status.context %>

The names of your columns can be found in db/schema.rb

If you want to rename the column in your database: http://stackoverflow.com/questions/1992019/how-can-i-rename-a-database-column-in-a-rails-migration

Glenn Harris
Glenn Harris
7,101 Points

Can you post the contents of your statuses controller? app/controlelrs/statuses_controller.rb

I'm guessing that after it creates the new status there are no instructions as to what it should do, e.g. (redirect to root path or redirect to @status).

this is the content of my controller:

class StatusesController < ApplicationController
  before_action :set_status, only: [:show, :edit, :update, :destroy]

  # GET /statuses
  # GET /statuses.json
  def index
    @statuses = Status.all
  end

  # GET /statuses/1
  # GET /statuses/1.json
  def show
  end

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

  # GET /statuses/1/edit
  def edit
  end

  # POST /statuses
  # POST /statuses.json
  def create
    @status = Status.new(status_params)

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

  # PATCH/PUT /statuses/1
  # PATCH/PUT /statuses/1.json
  def update
    respond_to do |format|
      if @status.update(status_params)
        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.destroy
    respond_to do |format|
      format.html { redirect_to statuses_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_status
      @status = Status.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def status_params
      params.require(:status).permit(:name, :context)
    end
end
Glenn Harris
Glenn Harris
7,101 Points

Hmmm...looks ok to me. Can you post the entire error message it returns? Or if you have it uploaded to github can you post the link?

OMG thanks so much. I cant believe I was so careless.