我创建了user-> posts->评论关联,但评论无法正常工作。当我评论帖子时,评论保存到数据库,列user_id被记录(确定),但列post_id为空
有部分_post_list.html.erb
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<%= render 'shared/comment' %>
<%= post.user.name %>
</tr></br></br>
<% end %>
有部分_comment.html.erb
<%= form_for(@comment) do |f| %>
<%= f.text_area :body %><br><br>
<%= f.submit "commented" %>
<% end %>
controller comments_controller
class CommentsController < ApplicationController
def new
@comment = Comment.new
end
def create
@comment = current_user.comments.build(comment_params)
if @comment.save
redirect_to root_url
else
render 'static_pages/home'
end
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
UPD 有服务器日志
Started POST "/posts/6/comments" for 127.0.0.1 at 2014-11-16 23:28:26 +0300
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZvyHKxSdH0a1rf79yaWmo9N/pD9/YhSQ9Ek8bdMLhOI=", "comment"=>{"body"=>"stackoverflow"}, "commit"=>"commented", "post_id"=>"6"}
User Load (4.9ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'b52623cb00671708536fce96c310b5d365128880' LIMIT 1
(0.2ms) begin transaction
SQL (0.6ms) INSERT INTO "comments" ("body", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["body", "stackoverflow"], ["created_at", "2014-11-16 20:28:26.544500"], ["updated_at", "2014-11-16 20:28:26.544500"], ["user_id", 5]]
(131.7ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 152ms (ActiveRecord: 137.4ms)
Started GET "/" for 127.0.0.1 at 2014-11-16 23:28:26 +0300
Processing by StaticPagesController#home as HTML
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'b52623cb00671708536fce96c310b5d365128880' LIMIT 1
Rendered shared/_post_form.html.erb (2.4ms)
Post Load (0.4ms) SELECT "posts".* FROM "posts" ORDER BY created_at DESC
Rendered shared/_comment.html.erb (1.8ms)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 5]]
Rendered shared/_comment.html.erb (2.8ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_comment.html.erb (4.0ms)
CACHE (3.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_comment.html.erb (1.5ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_comment.html.erb (2.9ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_post_list.html.erb (45.3ms)
Rendered static_pages/home.html.erb within layouts/application (52.1ms)
Rendered layouts/_header.html.erb (1.2ms)
Completed 200 OK in 364ms (Views: 347.0ms | ActiveRecord: 4.5ms)
controller static_pages_controller class StaticPagesController&lt; ApplicationController的 def指数 端
def show
end
def home
@post = current_user.posts.build
if signed_in?
end
@posts = Post.all
@comment = current_user.comments.build
end
end
查看home.html.erb
<%= render 'shared/post_form' %>
<%= render 'shared/post_list' %>
怎么修复? 抱歉我的英文不好
答案 0 :(得分:1)
您远未显示足够的代码,以便我们了解可能发生的情况。但我觉得很奇怪,你的表单不包含对post.id
的引用,或者你为post对象标识的名称。
控制器只能使用:
如果当前的帖子ID既不在表单字段中,也不在会话中,则控制器不会获取id,也不能将其存储在数据库中。
答案 1 :(得分:0)
看起来您正在遍历所有帖子并在同一页面上为每个帖子创建评论表单。您可能希望将评论路由设置为嵌套资源,方法是将其包装在post资源中。
# config/routes.rb
resources :posts do
resources :comments
end
将你的@post对象传递给部分,如:
<%= render 'shared/comment', post: post %>
然后您应该可以像这样创建评论表单:
<%= form_for([post,@comment]) do |f| %>
<%= f.text_area :body %><br><br>
<%= f.submit "commented" %>
<% end %>
通过添加post
,您正在告知评论它属于哪个帖子。如果您正在使用它们,请不要忘记更新控制器中的强参数。
编辑:根据您的更新,您需要将:post_id
添加到控制器底部的许可证中。