undefined局部变量或方法`new_comment`

时间:2022-02-22 11:55:55

NameError in Comments#create

注释#create中的NameError

Showing /comments/_form.html.erb where line #1 raised:

显示/comments/_form.html.erb,其中第1行引发:

undefined local variable or method new_comment for #<#:0x007fb9760eb640>

未定义的局部变量或##:0x007fb9760eb640>的方法new_comment

<%= form_for new_comment, url: send(create_url, new_comment.commentable) do |f| %>

new_comment is being called by:

new_comment被调用:

activities/index.html.erb

<%= render "comments/form", new_comment: Comment.new(commentable_id: activity.id, commentable_type: activity.class.model_name), create_url: :activity_comments_path %>

<%= render“comments / form”,new_comment:Comment.new(commentable_id:activity.id,commentable_type:activity.class.model_name),create_url :: activity_comments_path%>

comment.rb

class Comment < ActiveRecord::Base
  after_save :create_notification #ERROR OCCURRED WHEN I INTRODUCED THIS LINE AND
  validates :activity, presence: true #THIS LINE
  validates :user, presence: true
  has_many :notifications
  belongs_to :commentable, polymorphic: true
  belongs_to :user
  belongs_to :activity

private

  def create_notification
    Notification.create(
      activity_id: self.activity_id, 
      user_id: self.user_id, 
      comment_id: self.id, 
      read: false
      )
  end
end

notification.rb

class Notification < ActiveRecord::Base
  belongs_to :activity
  belongs_to :comment
  belongs_to :user
end

activity.rb

class Activity < ActiveRecord::Base
  self.per_page = 20
  has_many :notifications
  belongs_to :user
  has_many :comments, as: :commentable
  belongs_to :trackable, polymorphic: true

  def conceal
    trackable.conceal
  end

  def page_number
    (index / per_page.to_f).ceil
  end

private

  def index
    Activity.order(created_at: :desc).index self
  end
end

routes.rb

resources :activities do
  resources :comments
  resources :notifications
  member do
    post :like
    post :notifications
  end
end

Any ideas on what maybe the cause? This error is coming off of the awesome answer here: How to make a path to a paginated url?

关于可能原因的任何想法?这个错误来自这里很棒的答案:如何创建一个分页网址的路径?

Thank you!

UPDATE

class CommentsController < ApplicationController
    before_action :load_commentable
  before_action :set_comment, only: [:show, :edit, :update, :destroy, :like]
  before_action :logged_in_user, only: [:create, :destroy]

    def index
        @comments = @commentable.comments
    end

    def new
        @comment = @commentable.comments.new
    end

    def create
        @comment = @commentable.comments.new(comment_params)
        if @comment.save
            redirect_to @commentable, notice: "comment created."
        else
            render :new
        end
    end

    def edit
        @comment = current_user.comments.find(params[:id])
    end

    def update
        @comment = current_user.comments.find(params[:id])
        if @comment.update_attributes(comment_params)
            redirect_to @commentable, notice: "Comment was updated."
        else
            render :edit
        end
    end

    def destroy
        @comment = current_user.comments.find(params[:id])
        @comment.destroy
        redirect_to @commentable, notice: "comment destroyed."
    end

  def like
    @comment = Comment.find(params[:id])
    @comment_like = current_user.comment_likes.build(comment: @comment)
    if @comment_like.save
            @comment.increment!(:likes)
        flash[:success] = 'Thanks for liking!'
    else
        flash[:error] = 'Two many likes'
      end  
        redirect_to(:back)
  end

private

  def set_comment
    @comment = Comment.find(params[:id])
  end

    def load_commentable
        resource, id = request.path.split('/')[1, 2]
        @commentable = resource.singularize.classify.constantize.find(id)
    end

    def comment_params
        params[:comment][:user_id] = current_user.id
        params.require(:comment).permit(:content, :commentable, :user_id, :like)
    end
end

notifications_controller

class NotificationsController < ApplicationController
    def index
    @notifications = current_user.notifications
    @notifications.each do |notification|
        notification.update_attribute(:read, true)
    end
    end


    def destroy
      @notification = Notification.find(params[:id])
      @notification.destroy
      redirect_to :back
    end
end

activities_controller

class ActivitiesController < ApplicationController
    def index
        @activities = Activity.order("created_at desc").paginate(:page => params[:page])
    end

    def show
          redirect_to(:back)
    end

  def like
    @activity = Activity.find(params[:id])
    @activity_like = current_user.activity_likes.build(activity: @activity)
    if @activity_like.save
      @activity.increment!(:likes)
      flash[:success] = 'Thanks for liking!'
    else
      flash[:error] = 'Two many likes'
    end  
      redirect_to(:back)
  end
end

1 个解决方案

#1


It should be <%= form_for comment do |f| %> not new comment since comment is the element in your database

它应该是<%= form_for comment do | f | %>不是新评论,因为comment是数据库中的元素

#1


It should be <%= form_for comment do |f| %> not new comment since comment is the element in your database

它应该是<%= form_for comment do | f | %>不是新评论,因为comment是数据库中的元素