저에게 이것은 이상한 것입니다.요청 매개 변수가 내가 수행한다고해도 ID가없는 게시물을 찾을 수 없습니다
나는 2 가지 모델 (사용자 포함)을 가지고 있으며 게시물이 있으며 의견이 있습니다. 내가하려고하는 것은 post # show보기에 form_for 주석을 넣는 것입니다.
그러나 내가 어떤 의견을 제시 할 때 ID없이 게시물을 찾을 수 없었습니다. 내가 요청 매개 변수를 볼 때 내가 볼 수 있지만 : 명확하게에 - 사실 post_id를 얻을 않는 것을 알 수
{"utf8"=>"✓", "authenticity_token"=>"7bAXF66sghTKAF7b61gu08hElC+O1nR6RoT92tqQGOI=", "comment"=>{"content"=>"ok"}, "commit"=>"Add comment", "action"=>"create", "controller"=>"comments", "post_id"=>"23"}
그리고 수많은 시간 후 23
의 ID는이 경우에 있음 나는 너희들에게 해결책이 있는지 알 것이라고 생각했다.
내 comments_controller.rb :
class CommentsController < ApplicationController
before_action :load_post
def create
@comment = @post.comments.build(params[:content])
@comment.user = current_user
if @comment.save
@comment.create_activity :create, owner: current_user
redirect_to root_url, notice: "Comment was created."
else
render :new
end
def load_post
@post = Post.find(params[:id])
end
end
내 posts_controller.rb 양식에 대한
def show
@post = Post.find(params[:id])
@comment = Comment.new
end
부분 의견/_form.rb
<%= form_for [@post, @comment] do |f| %>
<%= f.text_area :content %>
<%= f.submit "Add comment" %>
<% end %>
,363,210
내 routes.rb
resources :posts do resources :comments end
내 게시물
/show.html.erb<%= render @post %>
<h3>New comment</h3>
<%= render 'comments/form' %>
내 게시물/_post.html.erb
<h2><%= @post.title %></h2>
<p><%= @post.content %></p>
<em>written by <%= @post.user.fullname %></em>
대, 저장하지 않는 것
액세스에 관한 the : 내용. 그러나 id, user_id 및 post_id를 저장합니다. 이견있는 사람? – sja
도움이 되었다면 답을 틱 수 있습니다. 질문에 대해서는 A를 업데이트했습니다.를 선택하십시오. – Nithin