【发布时间】:2015-09-07 23:43:38
【问题描述】:
我正在尝试使用 simple_form 创建一个简单的博客应用程序
在我的 new.html.erb for Post 中,我有以下代码
<h3>Write your blog post here</h3>
<%= simple_form_for @post do |f| %>
<%= f.input :content %><br>
<%= f.submit %>
<% end %>
在 index.html.erb 中,我得到了
<%= @posts.each do |post| %>
<p>Post: <%= post.content %></p>
<% end %>
但是,我在 Post 的索引页面上收到以下内容
[#<Post id: 1, created_at: "2015-09-07 23:07:54", updated_at: "2015-09-07 23:07:54", content: nil>, #<Post id: 2, created_at: "2015-09-07 23:07:54", updated_at: "2015-09-07 23:07:54", content: nil>, #<Post id: 3, created_at: "2015-09-07 23:08:11", updated_at: "2015-09-07 23:08:11", content: nil>,
这是我的后期控制器
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
@post = Post.new(params[post_params])
if @post.save
success = true
message = "Nice!"
else
success = false
message = "Sucks!"
end
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:content)
end
end
这是我的模型
class Post < ActiveRecord::Base
belongs_to :user
end
最后,将内容列添加到 Post 的迁移
class AddContentToPosts < ActiveRecord::Migration
def change
add_column :posts, :content, :string
end
end
在我的帖子中输入文本后,我可以创建它。但我不确定它是否被保存为内容为“nil”。这非常令人沮丧,因为这似乎是一个非常简单的问题。经过数小时试图弄清楚发生了什么,在这里寻求帮助...... 谢谢!
【问题讨论】: