2016-07-18 11 views
0

私はすべてのコメントをhas_many_throughアソシエーション内のすべてのユーザーの投稿に表示しようとしています。rails 4未定義のローカル変数レンダリング部分has_many_through

routes.rbを:

resources :posts do 
    resources :comments 
end 

モデル:

class User < ActiveRecord::Base 
    has_many :posts, dependent: :destroy 
    has_many :comments, :through => :posts, dependent: :destroy 
end 

class Post < ActiveRecord::Base 
    belongs_to :user 
    has_many :comments, dependent: :destroy 
end 

class Comment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :post 
end 

users_controller.rb

def activity 
    @user = current_user 
    @posts = @user.posts 
    @comments = @user.comments 
end 

activity.html.erb

<h4>My Activity</h4> 
<ol class="posts"> 
    <%= render @comments.last(3) %> 
</ol> 

_comment.html.erb

<div class="comment" id="comment-<%= post.id %>-<%= comment.id %>"> 
    <%= link_to smavatar_for(comment.user), user_path(comment.user.name) %> 
    <span class="user"><%= link_to comment.user.name, user_path(comment.user.name) %></span> 
    <div class="comment-content"> 
    <%= simple_format(comment.content) %> 
    </div> 
</div> 

activity.html.erbの行<%= render @comments.last(3) %>はのためにクラス

のためにエラーに

未定義のローカル変数やメソッド `ポスト」を与えます_comment.html.erbの<div class="comment" id="comment-<%= post.id %>-<%= comment.id %>">行私はおそらく部分的にローカル変数を渡す必要があることを知っている、私はちょうどそれを行う方法を把握することはできません。 <%= render @comments.last(3) %>の代わりに<%= @comments.last(3) %>を指定すると、ビューにすべてのコメントパラメータが出力されるので、コレクションを認識します。 locals: { comment: @comment, post: @post }を追加すると、クラスの未定義のローカル変数ポストが得られ、post: @comment.postはnilClassエラーを取得します。私はSOとRoRガイドの前後に行ってきましたが、部分的にレンダリングすることについては後ろ向きにしていますが、私はまだ何を渡すべきかについてはまだ不明です。

+0

同じページに投稿を表示していますか?私はあなたがコメントをどのようにレンダリングするかについては何の問題も見ません。 – mrvncaragay

+0

@ Marv-C、私は同じページに投稿をレンダリングできます。レンダリングはうまくいきますが、このビューはユーザーが他のユーザーから受け取ったコメントをレンダリングするだけです彼らの投稿。 –

答えて

2

リファレンスComment自体

からPostbelongs_to :post関連

comment.postを使用し、あなたが部分的内部

<%= render @comments.last(3) %> 

を持っていたとおりに、ショートカット構文を使用して、部分的に連絡したすべてのCommentを想定すると、またはcomment.post_idを取得するPostデータ

など。

<div class="comment" id="comment-<%= comment.post_id %>-<%= comment.id %>"> 
    <%= link_to smavatar_for(comment.user), user_path(comment.user.name) %> 
    <span class="user"><%= link_to comment.user.name, user_path(comment.user.name) %></span> 
    <div class="comment-content"> 
    <%= simple_format(comment.content) %> 
    </div> 
</div> 

このシナリオでは、あなたがlocalsオプションを通じてで@postを渡そうとしました

お知らせをlocalsを使用する必要はありません、しかし、あなたのコントローラにあなたが@posts変数(複数)が、ない@post単数形。

Commentレコードのそれぞれが異なるPostに属する可能性があるため、Postに渡すことのできるオプションはありません。

localsを使用する他の唯一の方法は、部分的にループを作り出し、それぞれCommentを個別にレンダリングする場合です。

<% @comments.last(3).each do |comment| %> 
    <%= render comment, post: comment.post %> 
    <!-- OR --> 
    <%= render partial: "comments/comment", locals: { comment: comment, post: comment.post } %> 
<% end %> 

通知は、我々はまだCommentに関連付けられているPostを取得するためにcomment.postを呼び出す必要があります。このため、ショートカット構文を使用して、部分的にCommentPostを参照するほうがはるかに簡単です。あなただけのパスlocalsなし_comment.html.erbにcomment.post.idpost.idを変更する必要が

+0

ラミエレス3世、ありがとう、そのトリックをしました。 –

+0

@JimHogan喜んで助けることができました! –

0

別の視点、それよりclearnerとシンプル

<div class="comment" id="comment-<%= comment.post.id %>-<%= comment.id %>"> 
    <%= link_to smavatar_for(comment.user), user_path(comment.user.name) %> 
    <span class="user"><%= link_to comment.user.name, user_path(comment.user.name) %></span> 
    <div class="comment-content"> 
    <%= simple_format(comment.content) %> 
    </div> 
</div> 

それは動作しますか?

+0

あまりにも悪い、私はレースを失う:D – alxibra

関連する問題