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

Treebook Rails app: Can't post statuses

when I got to localhost/statuses/new and attempt to post new status and input the fields, I get the following error.

New status 3 errors prohibited this status from being saved: Content can't be blank Content is too short (minimum is 2 characters) User can't be blank

I can't create status via console either. I would get this error in the console: WARNING: Can't mass-assign protected attributes for Status: content

What did I miss? Thanks.

Here is statuses controller:

 class StatusesController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :update, :edit]
  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)
    if @status.save
        redirect_to @status, notice: 'Status was successfully created.' 
    else
      render :new
    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(:user_id, :content)
    end
end

Here is my view for statuses/new

<%= simple_form_for @status 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, collection: User.all, label_method: :full_name %>

    <%= f.input :content %>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

4 Answers

Also, if you are having trouble with with the Database (Statuses) try running rake db:test:prepare in your terminal. Followed by rake db:migrate.

If you are then still not able to fix your statuses, I recommend deleting all of your current statuses from your database by logging into rails console then typing Status.delete_all This will clear out all of your statues. Then do the rake db:test:prepare, and then rake db:migrate to reload your test database.

Thanks for the response. following your suggestions, the problem is now fixed and I can post statuses. I'm using Rails4, which moved attr_accessible attributes from models to controller for security reasons. That's why I was reluctant to put them there. for the attributes to be accessible from the model, I had to add this line to my gemfile to allow me to add attr_acc in my model:

gem 'protected_attributes'

I added this line to my models/status.rb as you suggested.

attr_accessible :content, :user_id, :created_at, :updated_at

Here is what my models/status.rb looks like now.

class Status < ActiveRecord::Base

    attr_accessible :content, :user_id, :created_at, :updated_at

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

can you post your statuses model? seems you haven't the attribute accessible called content.

I also had this issue, here is a link to what helped me with my fix: https://teamtreehouse.com/forum/mass-assignment-error