사용자가 내 게시물에 대해 의견을 제시 할 수있는 방법을 만들고 싶습니다. 현재 내 홈 페이지에 모든 사용자 게시물이 표시되고 현재 사용자 프로필에만 사용자 프로필이 표시됩니다. 나는 의견을 사용자 프로필의 게시물에만 표시되도록하고 싶습니다. 나는 사용자 프로필에 코멘트 폼을 추가하려고 시도했지만 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>
같은 오류가 계속 발생했습니다. –