Ruby on rails 事件中的命名错误#show |未定义的方法` firstname';零级:零级

Ruby on rails 事件中的命名错误#show |未定义的方法` firstname';零级:零级,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我对RubyonRails非常陌生,如果我下面的问题非常基本,我会提前道歉,如果有任何帮助,我将不胜感激 继“railscast#154多态关联”之后,我现在能够为活动和博客创建评论 但我想做的是,当用户发表评论时,用户(发表评论的用户)的名字会与他们发表的评论一起出现……如下所示 详细内容如下-在我的视图/comments/_comments.html.erb文件中,我在添加了@comments.user=current_user的索引方法中放置了&controllers/comments\u

我对RubyonRails非常陌生,如果我下面的问题非常基本,我会提前道歉,如果有任何帮助,我将不胜感激

  • 继“railscast#154多态关联”之后,我现在能够为活动和博客创建评论

  • 但我想做的是,当用户发表评论时,用户(发表评论的用户)的名字会与他们发表的评论一起出现……如下所示

  • 详细内容如下-在我的视图/comments/_comments.html.erb文件中,我在添加了@comments.user=current_user的索引方法中放置了&controllers/comments\u controller.rb,但无法让它显示当前_用户名和他们发布的注释,如下所示

  • 艾玛·汤普森这是一个伟大的事件,非常享受

    emma thompson是目前的_用户

    大事,好好享受吧!作为评论

    -问题:如何调用发表评论的用户的姓名?

    错误消息

    NoMethodError in Events#show
    Showing /Users/ARTLoe/00_GitH/00_Projects/spefz_app/app/views/comments/_comments.html.erb where line #5 raised:
    undefined method `firstname' for nil:NilClass
    
        <ul>
        <% @comments.each do |comment| %>
          <li>
            <%= comment.user.firstname %>          #<--------------error appears on this line
            <%= simple_format comment.content %>
          </li>
        <% end %>
    
    型号

    class Comment < ActiveRecord::Base
      belongs_to :commentable, polymorphic: true
      belongs_to :user
    end
    
    class Event < ActiveRecord::Base
      belongs_to :user
      has_many :comments, as: :commentable
    end
    
    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable  
      has_many :events
      has_many :comments, as: :commentable
    end
    
    #<<<COMMENT CONTROLLER
    class CommentsController < ApplicationController
      before_action :set_comment, only: [:show, :edit, :update, :destroy]
      before_filter :load_commentable
    
      def index
        @comments = @commentable.comments
        @comments.user = current_user
      end
    
      def show
      end
    
      def new
        @comment = @commentable.comments.new
      end
    
      def edit
      end
    
      def create
        @comment = @commentable.comments.new(comment_params)
    
        respond_to do |format|
          if @comment.save
            format.html { redirect_to [@commentable], notice: 'Comment was successfully created.' }
            format.json { render :show, status: :created, location: @comment }
          else
            format.html { render :new }
            format.json { render json: @comment.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def update
        respond_to do |format|
          if @comment.update(comment_params)
            format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
            format.json { render :show, status: :ok, location: @comment }
          else
            format.html { render :edit }
            format.json { render json: @comment.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def destroy
        @comment.destroy
        respond_to do |format|
          format.html { redirect_to events_url, notice: 'Comment was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_comment
          @comment = Comment.find(params[:id])
        end
    
        # Never trust parameters from the scary internet, only allow the white list through.
        def comment_params
          params.require(:comment).permit(:content, :user_id)
        end
    
        def load_commentable
          resource, id = request.path.split('/')[1, 2]
          @commentable = resource.singularize.classify.constantize.find(id)
        end
    end
    
    
    #<<<EVENT CONTROLLER
    class EventsController < ApplicationController
      before_action :set_event, only: [:show, :edit, :update, :destroy]
      before_filter :authenticate_user!
    
      def index
        @events = Event.order(:date)
        # @events = current_user.events | displays only events by current user
      end
    
      def show
        @commentable = @event
        @comments = @commentable.comments
        @comment = Comment.new
      end
    
      def new
        @event = Event.new
      end
    
      def edit
      end
    
      def create
        @event = Event.new(event_params)
    
        respond_to do |format|
          if @event.save
            format.html { redirect_to @event, notice: 'Event was successfully created.' }
            format.json { render :show, status: :created, location: @event }
          else
            format.html { render :new }
            format.json { render json: @event.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def update
        respond_to do |format|
          if @event.update(event_params)
            format.html { redirect_to @event, notice: 'Event was successfully updated.' }
            format.json { render :show, status: :ok, location: @event }
          else
            format.html { render :edit }
            format.json { render json: @event.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def destroy
        @event.destroy
        respond_to do |format|
          format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      private
        def set_event
          @event = Event.find(params[:id])
        end
    
        def event_params
          params.require(:event).permit(:name, :description, :date, :time, :city, :price)
        end
    end
    
    class注释
    控制器

    class Comment < ActiveRecord::Base
      belongs_to :commentable, polymorphic: true
      belongs_to :user
    end
    
    class Event < ActiveRecord::Base
      belongs_to :user
      has_many :comments, as: :commentable
    end
    
    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable  
      has_many :events
      has_many :comments, as: :commentable
    end
    
    #<<<COMMENT CONTROLLER
    class CommentsController < ApplicationController
      before_action :set_comment, only: [:show, :edit, :update, :destroy]
      before_filter :load_commentable
    
      def index
        @comments = @commentable.comments
        @comments.user = current_user
      end
    
      def show
      end
    
      def new
        @comment = @commentable.comments.new
      end
    
      def edit
      end
    
      def create
        @comment = @commentable.comments.new(comment_params)
    
        respond_to do |format|
          if @comment.save
            format.html { redirect_to [@commentable], notice: 'Comment was successfully created.' }
            format.json { render :show, status: :created, location: @comment }
          else
            format.html { render :new }
            format.json { render json: @comment.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def update
        respond_to do |format|
          if @comment.update(comment_params)
            format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
            format.json { render :show, status: :ok, location: @comment }
          else
            format.html { render :edit }
            format.json { render json: @comment.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def destroy
        @comment.destroy
        respond_to do |format|
          format.html { redirect_to events_url, notice: 'Comment was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_comment
          @comment = Comment.find(params[:id])
        end
    
        # Never trust parameters from the scary internet, only allow the white list through.
        def comment_params
          params.require(:comment).permit(:content, :user_id)
        end
    
        def load_commentable
          resource, id = request.path.split('/')[1, 2]
          @commentable = resource.singularize.classify.constantize.find(id)
        end
    end
    
    
    #<<<EVENT CONTROLLER
    class EventsController < ApplicationController
      before_action :set_event, only: [:show, :edit, :update, :destroy]
      before_filter :authenticate_user!
    
      def index
        @events = Event.order(:date)
        # @events = current_user.events | displays only events by current user
      end
    
      def show
        @commentable = @event
        @comments = @commentable.comments
        @comment = Comment.new
      end
    
      def new
        @event = Event.new
      end
    
      def edit
      end
    
      def create
        @event = Event.new(event_params)
    
        respond_to do |format|
          if @event.save
            format.html { redirect_to @event, notice: 'Event was successfully created.' }
            format.json { render :show, status: :created, location: @event }
          else
            format.html { render :new }
            format.json { render json: @event.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def update
        respond_to do |format|
          if @event.update(event_params)
            format.html { redirect_to @event, notice: 'Event was successfully updated.' }
            format.json { render :show, status: :ok, location: @event }
          else
            format.html { render :edit }
            format.json { render json: @event.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def destroy
        @event.destroy
        respond_to do |format|
          format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      private
        def set_event
          @event = Event.find(params[:id])
        end
    
        def event_params
          params.require(:event).permit(:name, :description, :date, :time, :city, :price)
        end
    end
    

    #你就快到了。如果您始终希望创建操作的用户是当前用户,则需要在创建操作中设置该用户

    def create
      @comment = @commentable.comments.new(comment_params)
      @comment.user = current_user
    
      respond_to do |format|
        if @comment.save
          format.html { redirect_to [@commentable], notice: 'Comment was successfully created.' }
          format.json { render :show, status: :created, location: @comment }
        else
          format.html { render :new }
          format.json { render json: @comment.errors, status: :unprocessable_entity }
        end
      end
    end
    

    此外,您还需要删除索引操作上的
    @comments.user=current\u user
    。您希望以视图中已有的方式阅读它:
    comment.user.firstname
    ,而不是在索引操作中写入它(实际上这不会做任何事情,因为@comments是注释列表的一个关系,您无论如何都不能将每个注释都设置为那样)

    我很确定,在您的索引操作中,
    @comments.user=current\u user
    会给您一个错误。您在那里有一个活动记录集合,并且正在尝试为其设置一个值。一方面,索引操作不应该在模型上设置属性,另一方面,如果要设置值,则需要在每个单独的模型上设置。见@tehfailsafe的答案。