Rails条评论不保存
Rails comments do not save
编辑:从表单提交记录:
Started POST "/posts/1/comments" for 141.161.133.10 at 2015-03-19 17:10:19 +0000
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9laIJxseq/x+7C3iJAdpvalFvMgN397lWC7c/6Ha/hEjKTE9A3e19liFlwGzZjePReFdh2F1fLj1itZzRjixgQ==", "comment"=>{"body"=>"Hi Whosebug"}, "commit"=>"Submit", "post_id"=>"1"}
(0.3ms) begin transaction
SQL (1.9ms) INSERT INTO "comments" ("body", "created_at", "updated_at") VALUES (?, ?, ?) [["body", "Hi Whosebug"], ["created_at", "2015-03-19 17:10:19.808006"], ["updated_at", "2015-03-19 17:10:19.808006"]]
(32.1ms) commit transaction
Redirected to https://blog-erikvdw.c9.io/posts.10
Completed 302 Found in 48ms (ActiveRecord: 34.2ms)
和comment_params方法:
def comment_params
params.require(:comment).permit(:body)
end
我已经在 github repo 和其他与我正在做的类似的 SO 问题中环顾了很长一段时间,但还没有确切地看到我做错了什么。我的问题是在 Posts/Show 中提交评论表单时未创建评论。我认为这是 activerecord 关联的问题:
Posts/Show
<% content_for(:title, @post.title ) %>
<p id="notice"><%= notice %></p>
<h1>Blog</h1>
<br>
<div>
<%= image_tag @post.image.full.url %>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h2><em><%= @post.title %></em></h2>
</div>
<div class="panel-body">
<p><%= @post.content %></p>
</div>
</div>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
<% @post.comments.each do |comment| %>
<%= comment.body %>
<% end %>
<%= form_for [@post, @post.comments.build] do |f| %>
<%= f.text_area :body %>
<%= f.submit "Submit" %>
<% end %>
<p>Comments: <%= @post.comments.count %></p>
帖子模型
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
mount_uploader :image, PostUploader
end
评论模型
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
end
架构文件
create_table "comments", force: :cascade do |t|
t.string "body"
t.integer "user_id"
t.integer "employee_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "post_id"
end
add_index "comments", ["employee_id"], name: "index_comments_on_employee_id"
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
add_index "comments", ["user_id"], name: "index_comments_on_user_id"
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "username"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
Comments_Controller 新建和创建操作
def new
@comment = Comment.new
end
# POST /comments
# POST /comments.json
def create
@comment = Comment.new(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to posts_path(@comment), notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
您不是通过特定 Post 创建评论,因此没有关联。在您的创建操作中试试这个:
@comment = Post.find(params[:post_id]).comments.new(comment_params)
这将在您的评论 table 中自动填充 post_id,然后 link 将它添加到 Post。
编辑:从表单提交记录:
Started POST "/posts/1/comments" for 141.161.133.10 at 2015-03-19 17:10:19 +0000
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9laIJxseq/x+7C3iJAdpvalFvMgN397lWC7c/6Ha/hEjKTE9A3e19liFlwGzZjePReFdh2F1fLj1itZzRjixgQ==", "comment"=>{"body"=>"Hi Whosebug"}, "commit"=>"Submit", "post_id"=>"1"}
(0.3ms) begin transaction
SQL (1.9ms) INSERT INTO "comments" ("body", "created_at", "updated_at") VALUES (?, ?, ?) [["body", "Hi Whosebug"], ["created_at", "2015-03-19 17:10:19.808006"], ["updated_at", "2015-03-19 17:10:19.808006"]]
(32.1ms) commit transaction
Redirected to https://blog-erikvdw.c9.io/posts.10
Completed 302 Found in 48ms (ActiveRecord: 34.2ms)
和comment_params方法:
def comment_params
params.require(:comment).permit(:body)
end
我已经在 github repo 和其他与我正在做的类似的 SO 问题中环顾了很长一段时间,但还没有确切地看到我做错了什么。我的问题是在 Posts/Show 中提交评论表单时未创建评论。我认为这是 activerecord 关联的问题:
Posts/Show
<% content_for(:title, @post.title ) %>
<p id="notice"><%= notice %></p>
<h1>Blog</h1>
<br>
<div>
<%= image_tag @post.image.full.url %>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h2><em><%= @post.title %></em></h2>
</div>
<div class="panel-body">
<p><%= @post.content %></p>
</div>
</div>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
<% @post.comments.each do |comment| %>
<%= comment.body %>
<% end %>
<%= form_for [@post, @post.comments.build] do |f| %>
<%= f.text_area :body %>
<%= f.submit "Submit" %>
<% end %>
<p>Comments: <%= @post.comments.count %></p>
帖子模型
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
mount_uploader :image, PostUploader
end
评论模型
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
end
架构文件
create_table "comments", force: :cascade do |t|
t.string "body"
t.integer "user_id"
t.integer "employee_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "post_id"
end
add_index "comments", ["employee_id"], name: "index_comments_on_employee_id"
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
add_index "comments", ["user_id"], name: "index_comments_on_user_id"
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "username"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
Comments_Controller 新建和创建操作
def new
@comment = Comment.new
end
# POST /comments
# POST /comments.json
def create
@comment = Comment.new(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to posts_path(@comment), notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
您不是通过特定 Post 创建评论,因此没有关联。在您的创建操作中试试这个:
@comment = Post.find(params[:post_id]).comments.new(comment_params)
这将在您的评论 table 中自动填充 post_id,然后 link 将它添加到 Post。