首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法访问注释对象中的用户

无法访问注释对象中的用户
EN

Stack Overflow用户
提问于 2019-06-10 19:56:41
回答 3查看 63关注 0票数 0

我有以下模型,帖子,评论和用户。Comment对象属于user,用户有很多评论。但是,当尝试呈现视图以显示注释时,用户无法访问,并显示nil:NilClass的未定义方法“`avatar”。附加的头像是与User类相关联的方法。

这是comments控制器

代码语言:javascript
复制
class CommentsController < ApplicationController

    before_action :authenticate_user!, except: [:index, :show]
    before_action :set_commentable 

    def create
        @comment = @commentable.comments.new comment_params
        @comment.user = current_user
        @comment.save
        redirect_to @commentable, notice: "Comment Posted!"
    end

    def index
        @comments = @commentable.comments.all
        @comment = @commentable.comments.new comment_params
    end



    private

        def comment_params
            params.require(:comment).permit(:body)
        end

        def set_commentable
            @commentable = Post.find(params[:post_id])
        end

end

下面是评论模型:

代码语言:javascript
复制
class Comment < ApplicationRecord
    belongs_to :user
    belongs_to :commentable, polymorphic: true
end

下面是用户模型:

代码语言:javascript
复制
class User < ApplicationRecord

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  acts_as_voter

  has_many :posts
  has_many :comments
  has_one_attached :avatar


  validates :username, uniqueness: true
  validates :email, uniqueness: true

end

这是我试图呈现的视图。我希望评论显示在帖子的显示视图的底部:

代码语言:javascript
复制
<p id="notice"><%= notice %></p>

<div class="w3-container w3-card w3-white w3-round m7" style="margin:auto; display:table; min-width: 500px; max-width: 800px;"><br>
        <% if @post.user.avatar.attached? %>
            <%= image_tag @post.user.avatar, class: "w3-circle avatar w3-left w3-margin-right", :style => "display: inline;" %>
        <% else %>
        <img src="https://www.w3schools.com/w3images/avatar2.png" alt="Avatar" class="w3-left w3-circle w3-margin-right" style="width:60px">
        <% end %>
        <%= link_to @post do %>
        <span class="w3-right w3-opacity"><%= time_ago_in_words(@post.created_at) %> ago</span>
        <% end %>

        <h4><%= @post.user.name %></h4> <h5>@<%= @post.user.username%></h5>
        <hr class="w3-clear">
        <p><%= @post.content %></p>
        <% if user_signed_in? %>
        <hr class="w3-clear">
        <%= link_to like_post_path(@post), method: :put,  class: "w3-button w3-theme-d2 w3-margin-bottom" do%>
            <i class="fa fa-thumbs-up"></i>
            <span class="w3-badgeM"><%= @post.get_upvotes.size %></span>
        <% end %>    
        <button type="button" class="w3-button w3-theme-d2 w3-margin-bottom"><i class="fa fa-comment"></i></button>

        <% if current_user.id == @post.user_id %>
            <%= link_to edit_post_path(@post), class: "w3-button w3-theme-d1 w3-margin-bottom" do %>
                <i class="fas fa-pencil-alt"></i>
            <% end %>
            <%= link_to @post, method: :delete, data: {confirm: "Are you sure you want to delete this post?"}, class: "w3-button w3-theme-d1 w3-margin-bottom" do %>
                <i class="fas fa-trash-alt"></i>
            <% end %>
        <% end %>
        <% else %>
        <br >
        <% end %>

        <%= simple_form_for([@post, @post.comments.build]) do |f| %>
        <div class="field">
            <div class="control">
            <%= f.input :body, input_html: { class: 'textarea' }, wrapper: false, label_html: { class: 'label' }  %>
            </div>
        </div>
        <%= f.button :submit, 'Leave a reply', class: "button is-primary" %>
        <% end %>
        <% @post.comments.each do |comment|%>
        <div class="w3-container w3-card w3-round w3-margin" style="background-color: #f3f3f3; padding: 0px 5px 5px;" ><br>
            <% if comment.user.avatar.attached? %>
                <%= image_tag comment.user.avatar, class: "w3-circle avatar w3-left w3-margin-right", :style => "display: inline;" %>
            <% else %>
                <img src="https://www.w3schools.com/w3images/avatar2.png" alt="Avatar" class="w3-left w3-circle w3-margin-right" style="width:60px">
            <% end %>
            <% if user_signed_in? %>

            <% end %>
            <span class="w3-right w3-opacity"><%= time_ago_in_words(comment.created_at) %> ago</span>
            <h6><%= comment.user.name %></h6> <p><strong>@<%= comment.user.username%></strong></p>
            <p style="max-height: 75px; overflow:auto;"><%= comment.body %></p>

        </div>
        <% end %>

    </div>

routes.rb文件:

代码语言:javascript
复制
Rails.application.routes.draw do
  devise_for :users
  #  :controllers => {registrations: 'registrations'}
  resources :posts do
    member do
      put "like" => "posts#upvote"
      put "unlike" => "posts#downvote"
    end
    resources :comments
  end   
  root "posts#index"
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

然而,当我尝试渲染它时,我得到了以下错误:

如果我查看数据库,就会发现注释记录具有正确的ids:

在选择帖子后,当我尝试在控制台中运行头像附加方法时,它似乎返回true,并且没有抛出任何错误:

有没有人能帮帮我,指出我哪里出了问题?谢谢:)

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56525932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档