biversen21
biversen21

Reputation: 37

Trouble with rails tutorial

I am working through a Rails tutorial, which I am still very new to. The app is designed to simply show status posts comparable to Facebook. At some point I must have missed something, and can't figure out where. Basically the status content is no longer displaying for me.

Here is the index:

<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>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?'} %>
        </span>
    </div>
</div>
<% end %>

Here is the form:

<%= 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 |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

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

status.content is not displaying at all and I can't figure out why. Please let me know if I failed to include a file, I didn't want to include a ton of unnecessary code.

Thanks!

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 :show, status: :created, location: @status }
      else
        format.html { render :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 { render :show, status: :ok, location: @status }
      else
        format.html { render :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, notice: 'Status was successfully destroyed.' }
      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, :content)
    end
end

Log from terminal (I think this is it?)

Started POST "/statuses" for 127.0.0.1 at 2014-06-27 11:47:14 -0700
Processing by StatusesController#create as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"0e4j7GRZXbUSMcf+hOe69gJzjPnpRT/24p5W4T9/ccA=", "status"=>    {"content"=>"New status test"}, "commit"=>"Create Status"}
WARNING: Can't mass-assign protected attributes for Status: content
    app/controllers/statuses_controller.rb:27:in `create'
   (0.1ms)  begin transaction
  SQL (0.7ms)  INSERT INTO "statuses" ("created_at", "updated_at") VALUES (?, ?)      [["created_at", "2014-06-27 18:47:14.783419"], ["updated_at", "2014-06-27    18:47:14.783419"]]
   (1.0ms)  commit transaction
Redirected to http://0.0.0.0:3000/statuses/9
Completed 302 Found in 11ms (ActiveRecord: 1.8ms)

Status model:

class Status < ActiveRecord::Base
end

Upvotes: 0

Views: 51

Answers (1)

Pavan
Pavan

Reputation: 33552

You should be adding attr_accessible :content to your Status model in order to mass assign the attributes.

class Status < ActiveRecord::Base

attr_accessible :content

end

If you need to display the name attribute along with content,then change it to attr_accessible :name, :content.

Upvotes: 1

Related Questions