나는 user-> posts-> comments 연관을 만들지 만, 코멘트는 올바르게 작동하지 않는다. 나는 포스트에 주석을 달았을 때, 데이터베이스에 주석을 저장하고, user_id 열은 기록했지만 (ok), column post_id 비었다.왜 코멘트가 작동하지 않는가
부분 _post_list.html.erb있다 _comment.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 %>
<%= form_for(@comment) do |f| %>
<%= f.text_area :body %><br><br>
<%= f.submit "commented" %>
<% end %>
서버 로그
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있다 comments_controller 제어기
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)
클래스 StaticPagesController <와 ApplicationController 데프 지수 끝
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' %>
어떻게 해결 static_pages_controller 컨트롤러? 내 나쁜 영어로 죄송합니다.
컨트롤러 코드를 게시 할 수 있습니까? 그것이 속한'@ post'를'@ comment'라고 말하지 않습니다. 컨트롤러를 게시하면 무엇을 변경해야하는지 알려줄 수 있습니다. –
comments_controller 또는 posts_controller? – vveare138
개의 댓글이 생성 동작이있는 것으로 가정하고 –