我收到以下错误:
undefined method `email' for nil:NilClass
Extracted source (around line #6):
3: <div class="post-author">
4: <div class="post-author-info">
5: <span class="post-author-name">
6: <strong><%= link_to comment.user.email, comment.user %></strong>
7: </span>
8:
9: <span class="post-date secondary">
评论.rb:
def show
@comment = Comment.find(params[:id])
end
def create
@post = Post.find(params[:post_id])
comment_attr = params[:comment].merge :user_id => current_user.id
@comment = @post.comments.create(comment_attr)
redirect_to post_path(@post)
end
post.rb:
attr_accessible :title, :content
belongs_to :user
has_many :comments, :dependent => :destroy
用户.rb:
has_many :posts, :dependent => :destroy
has_many :comments, :dependent => :destroy
评论.rb:
attr_accessible :content, :user_id
belongs_to :post, :counter_cache => true
belongs_to :user
路线.rb:
resources :posts do
resources :comments
end
架构.rb:
create_table "comments", :force => true do |t|
t.text "content"
t.integer "post_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "provider"
t.string "uid"
end
我不确定有什么问题。上面的文件中的一切似乎都很好。
有什么建议么?
编辑:
帖子/show.html.erb:
<div id="post-<%= @post.id %>" class="post-single">
<h3 class="post-title"><%= @post.title %></h3>
<span class="post-author-name">
<strong><%= link_to @post.user.email, @post.user %></strong>
</span>
<span class="post-content">
<%= @post.content %>
</span>
<div class="comments">
<h4 class="comments-count"><%= pluralize(@post.comments_count, "Comment") %></h4>
<%= render @comments %>
<%= render 'comments/form' %>
</div>
</div>
评论/_comment.html.erb:
<div id="comment-<%= comment.id %>" class="comment">
<div class="post-author">
<div class="post-author-info">
<span class="post-author-name">
<strong><%= link_to comment.user.email, comment.user %></strong>
</span>
<span class="post-date secondary">
<%= time_ago_in_words(comment.created_at) %>
</span>
</div>
<span class="post-author-control">
<% unless current_user == nil %>
<% if current_user.id == comment.user_id %>
<%= link_to 'Edit', edit_post_comment_path(@post, comment) %>
<% end %>
<% end %>
</span>
</div>
<div class="comment-body">
<p><%= comment.content %></p>
</div>
</div>
帖子.rb:
def show
@post = Post.find(params[:id])
@comments = @post.comments
@comment = @post.comments.build
end
评论/_form.html.erb:
添加评论:<%= form_for([@post, @comment]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>