我正在完成一个Rails教程,我仍然很新。该应用程序旨在显示与Facebook相当的状态帖子。在某些时候我一定错过了什么,并且无法弄清楚在哪里。基本上,状态内容不再显示给我。
这是索引:
<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 %>
以下是表格:
<%= 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
根本没有显示,我无法弄清楚原因。如果我没有包含文件,请告诉我,我不想包含大量不必要的代码。
谢谢!
控制器:
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
从终端登录(我想是这样吗?)
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)
状态模型:
class Status < ActiveRecord::Base
end
答案 0 :(得分:1)
您应该将 attr_accessible :content
添加到 Status
模型中,以便批量分配属性。
class Status < ActiveRecord::Base
attr_accessible :content
end
如果您需要显示 name
属性以及 content
,请将其更改为 attr_accessible :name, :content
< /强>