2011-08-09 34 views
0

私はユーザーが自分の投稿にコメントする方法を作り出そうとしています。現在のところ、私のホームページにすべてのユーザー投稿が表示され、ユーザープロフィールには現在のユーザーだけが投稿されています。私はコメントがユーザーのプロファイルの投稿にのみ表示されるようにしたいと思います。私はユーザープロファイルにコメントフォームを追加しようとしましたが、Nilのための未定義のメソッド `comments 'があります:NilClassエラー。定義されていないメソッドnilのための `comments ':NilClass

私comments_controllerは私が私のコメントモデルが

のように見える

<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 %> 

のように見えるユーザプロファイルにレンダリングしています部分(_comment_form.html.erb)を持っている

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

のように見えます

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ですか?この質問からは明らかではなかった。しかし、あなたのコードから判断すると、2つの可能性のある場所があります。コードの最後の行が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。それがnullであると仮定すると、あなたはこの行ではなく@postを参照する必要があります_comment_form.html.erb

1
<%= render @posts %> 

をレンダリングどこPostオブジェクトをインスタンス化する必要があります。あなたのコード内のそれへの他のすべての参照と比較して、末尾にsがあることに注意してください。

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

ようこそ!スタックオーバーフロー!将来の参照のために必要となるため、投稿するコードには常に説明を記述してください。 –