2014-07-14 1 views
0

저에게 이것은 이상한 것입니다.요청 매개 변수가 내가 수행한다고해도 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> 

답변

3

당신에게 "post_id"은 (는)이 아닙니다. (210)

당신은 보안을 위해 비공개로 load_post 조치를 할 수

이 작동 듯하지만 또 다른 오류를 발생하는의 content

같은
def create 
@comment = @post.comments.build(params[:comment][:content]) 
.... 
+0

대, 저장하지 않는 것

private def load_post @post = Post.find(params[:post_id]) end 

액세스에 관한 the : 내용. 그러나 id, user_id 및 post_id를 저장합니다. 이견있는 사람? – sja

+0

도움이 되었다면 답을 틱 수 있습니다. 질문에 대해서는 A를 업데이트했습니다.를 선택하십시오. – Nithin

관련 문제