Html 如何显示某些帖子选项,仅针对Rails中帖子的创建者

Html 如何显示某些帖子选项,仅针对Rails中帖子的创建者,html,ruby-on-rails,ruby,activerecord,Html,Ruby On Rails,Ruby,Activerecord,我得到了nil:NilClass的错误undefined method user',但是我的代码看起来不错。我在Rails控制台中进行了检查,这些帖子确实有user_id`字段,并且按照预期工作 这是我的帖子模型: # == Schema Information # # Table name: posts # # id :integer not null, primary key # body :string # created_at :dat

我得到了nil:NilClass的错误undefined method user',但是我的代码看起来不错。我在Rails控制台中进行了检查,这些帖子确实有user_id`字段,并且按照预期工作

这是我的帖子模型:

# == Schema Information
#
# Table name: posts
#
#  id         :integer          not null, primary key
#  body       :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  user_id    :integer
#

class Post < ActiveRecord::Base
    validates :body, presence: true
    belongs_to :user
end
这是我的视图的一部分,我试图控制谁有权编辑和删除:

<% if signed_in? && @post.user == current_user %>

    <%= link_to "Edit", edit_post_path(post) %>
    <%= link_to "Delete", post, method: :delete, data: {confirm: "Are you sure you want to delete this post?"} %>
<%end%>
以下是错误跟踪:

Started GET "/" for ::1 at 2016-02-06 15:48:00 -0500
Processing by PostsController#index as HTML
  Post Load (0.3ms)  SELECT "posts".* FROM "posts"  ORDER BY "posts"."id" DESC
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered posts/index.html.erb within layouts/application (9.2ms)
Completed 500 Internal Server Error in 46ms (ActiveRecord: 0.4ms)

ActionView::Template::Error (undefined method `user' for nil:NilClass):
     9:                     <div class="post-container hyphenate">
    10:                         <%= post.body %>
    11: 
    12:                         <% if signed_in? && @post.user == current_user %>
    13: 
    14:                             <%= link_to "Edit", edit_post_path(post) %>
    15:                             <%= link_to "Delete", post, method: :delete, data: {confirm: "Are you sure you want to delete this post?"} %>
  app/views/posts/index.html.erb:12:in `block in _app_views_posts_index_html_erb__995985056227498542_70346416865540'
  app/views/posts/index.html.erb:8:in `_app_views_posts_index_html_erb__995985056227498542_70346416865540'
改变


索引页上没有@post。

您可以包含错误stacktrace及其引用的相关代码吗?@Jon,刚刚添加了它,谢谢。看起来@post没有设置。欢迎使用堆栈溢出。不要用所有的帽子,因为你在大喊大叫。该死的,我得把比萨饼从烤箱里拿出来,你在我前面咬了进去:潘乔伊,你的饭,@Jon
Started GET "/" for ::1 at 2016-02-06 15:48:00 -0500
Processing by PostsController#index as HTML
  Post Load (0.3ms)  SELECT "posts".* FROM "posts"  ORDER BY "posts"."id" DESC
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered posts/index.html.erb within layouts/application (9.2ms)
Completed 500 Internal Server Error in 46ms (ActiveRecord: 0.4ms)

ActionView::Template::Error (undefined method `user' for nil:NilClass):
     9:                     <div class="post-container hyphenate">
    10:                         <%= post.body %>
    11: 
    12:                         <% if signed_in? && @post.user == current_user %>
    13: 
    14:                             <%= link_to "Edit", edit_post_path(post) %>
    15:                             <%= link_to "Delete", post, method: :delete, data: {confirm: "Are you sure you want to delete this post?"} %>
  app/views/posts/index.html.erb:12:in `block in _app_views_posts_index_html_erb__995985056227498542_70346416865540'
  app/views/posts/index.html.erb:8:in `_app_views_posts_index_html_erb__995985056227498542_70346416865540'
<% if signed_in? && @post.user == current_user %>
<% if signed_in? && post.user == current_user %>