Ruby on rails 未定义的局部变量或方法“new_comment”`
注释中的名称错误#创建 显示/评论/_form.html.erb 第#1行出现的位置: 未定义的局部变量或方法# 通知.rbRuby on rails 未定义的局部变量或方法“new_comment”`,ruby-on-rails,ruby,Ruby On Rails,Ruby,注释中的名称错误#创建 显示/评论/_form.html.erb 第#1行出现的位置: 未定义的局部变量或方法# 通知.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,
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
class Notification < ActiveRecord::Base
belongs_to :activity
belongs_to :comment
belongs_to :user
end
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
resources :activities do
resources :comments
resources :notifications
member do
post :like
post :notifications
end
end
你知道原因是什么吗?这个错误来自于这里令人敬畏的答案:
谢谢强>
更新
class CommentsController
通知\u控制器
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
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
类通知控制器
活动\u控制器
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
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
类活动控制器params[:page])
结束
def秀
将_重定向到(:返回)
结束
def-like
@activity=activity.find(参数[:id])
@activity\u like=当前用户。activity\u like.build(activity:@activity)
如果@activity\u like.save
@活动。增量!(:喜欢)
flash[:success]=“谢谢喜欢!”
其他的
flash[:error]=“两个很多喜欢”
结束
将_重定向到(:返回)
结束
结束
应该是
而不是新注释,因为注释是数据库中的元素谢谢Zippo!这给了我活动中的namererror#索引定义的局部变量或该行的方法“comment”。它在实现我在comments.rb comments中提到的那两行之前就已经工作了。我从Joe的回答中得到了新的评论:@AnthonyGalli.com请发布你的控制器代码also@AnthonyGalli.com试试这个:
Nope@anusha,它给出了:NoMethodError in Activities.#索引显示/comments/_form.html.erb,其中第1行出现:nil:NilClass的未定义方法“commentable”
@AnthonyGalli.com什么关于此