2011-08-09 11 views
0

사용자가 내 게시물에 대해 의견을 제시 할 수있는 방법을 만들고 싶습니다. 현재 내 홈 페이지에 모든 사용자 게시물이 표시되고 현재 사용자 프로필에만 사용자 프로필이 표시됩니다. 나는 의견을 사용자 프로필의 게시물에만 표시되도록하고 싶습니다. 나는 사용자 프로필에 코멘트 폼을 추가하려고 시도했지만 nil 클래스에 대해 정의되지 않은 메소드`comments '가 있습니다. NilClass 에러입니다. 나는 부분 (_comment_form.html.erb)가 정의되지 않은 메소드 nil에 대한 주석 : NilClass

class CommentsController < ApplicationController 
    def create 
    @post = Post.find(params[:post]) 
    @comment = @post.comments.create(params[:comment]) 
    redirect_to post_path(@post) 
end 

처럼 내가

<h2>Add a comment:</h2> 
<%= form_for ([@post, @post.comments.build]) do |f| %> 
    <div class="field"> 
    <%= f.label :commenter %><br /> 
    <%= f.text_field :commenter %> 
    </div> 
    <div class="field"> 
    <%= f.label :body %><br /> 
    <%= f.text_area :body %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

내 의견 모델

모양처럼 보이는 사용자 프로필에 렌더링하고 그

내 comments_controller 보인다

class Comment < ActiveRecord::Base 
    belongs_to :post 
end 

내 게시물 모델은

와 같습니다.
class Post < ActiveRecord::Base 
attr_accessible :content 

belongs_to :user 

validates :content, :presence => true 
validates :user_id, :presence => true 
validates :user, :presence => true 
validates :title, :presence => true 

has_many :comments 

default_scope :order => 'posts.created_at DESC' 
end 

내 사용자 프로필 show.html.erb

<table class="profile" summary="Profile information"> 
    <tr> 
    <td class="main"> 
    <h1> 
     <%= gravatar_for @user %> 
     <%= @user.name %> 
    </h1> 
    <% unless @user.posts.empty? %> 
     <table class="posts" summary="User posts"> 
      <%= render @posts %> 
      <%= render 'comments/comment_form' %> 
     </table>  
    <% end %> 
    </td> 
    <td class="sidebar round"> 
    <strong>Name</strong> <%= @user.name %><br /> 
    <strong>URL</strong> <%= link_to user_path(@user), @user %><br /> 
    <strong>Tasks</strong> <%= @user.posts.count %> 
    </td> 
    </tr> 
</table> 

답변

2

그것은 당신이 컨트롤러의 new 방법에 @post를 초기화하지 않은이 nil로 사용되고 있음을 수 있습니다 같은데.

def new 
    @post = Post.new(params[:post]) 
end 
+0

같은 오류가 계속 발생했습니다. –

1

당신이 당신의가 PostsController의 show 액션에서 @post를 초기화하고 있습니까 : 그것은 실제의 경우 항상 새로운 양식 빈 모델을 구성? CommentsController의 작성 작업에서 리디렉션 할 것이므로이 작업이 필요합니다.

+0

그래, 내가 그 일을 시도했는데 내가 잘못하지 않았다면 여전히 바뀌지 않았다. 내가 한 일을 보여줬다 (at) post = post.find (params [: post]) (at) comments = (at) post.comments end –

+0

PostsController 또는 CommentsController에 show 액션 핸들러를 추가 했습니까?PostsController에 액션 핸들러를 추가해야합니다 (있는 경우) –

0

log/development.log 오류가 발생한 곳을 볼 수 있습니까? 이 질문에서 분명하지 않았다. 그러나 코드에서 판단, 두 가지 위치가 있습니다 : 코드의 마지막 줄은 id가 발견되지 않는 경우 RecordNotFound을 올릴 Post.find 때문에

여기
  1. @comment = @post.comments.create(params[:comment]) 는 않을

  2. <%= form_for ([@post, @post.comments.build]) do |f| %>

이것은 매우 가능성이 높습니다. puts @post.inspect을 수행하고 development.log에서 nu인지 확인할 수 있습니까? ll. 대신 @post을 참조해야합니다

1
<%= render @posts %> 

이 줄 _comment_form.html.erb 렌더링 곳이 널 (null)이라고 가정하면, 당신은 Post 개체를 인스턴스화해야합니다. 코드에서 다른 모든 참조와 비교하여 후행 s를 주목하십시오.

2
@post = Post.find_by_id(params[:post_id]) 
+2

Welcome to Stack Overflow! 나중에 참조 할 때 필요할 것이므로 항상 게시하는 코드에 설명을하십시오. –

관련 문제