gpt4 book ai didi

ruby-on-rails - 将方法添加到 Rails 中的模型时出现未定义的方法错误

转载 作者:行者123 更新时间:2023-12-04 16:40:06 25 4
gpt4 key购买 nike

我有错误

未定义的方法 events_and_repeat' for #<Class:0x429c840>
app/controllers/events_controller.rb:11:in `index'

我的应用程序/模型/event.rb 是

class Event < ActiveRecord::Base
belongs_to :user

validates :title, :presence => true,
:length => { :minimum => 5 }
validates :shedule, :presence => true

require 'ice_cube'
include IceCube

def events_and_repeats(date)
@events = self.where(shedule:date.beginning_of_month..date.end_of_month)

return @events
end

end

应用程序/ Controller /events_controller.rb
def index
@date = params[:month] ? Date.parse(params[:month]) : Date.today
@repeats = Event.events_and_repeats(@date)

respond_to do |format|
format.html # index.html.erb
format.json { render json: @events }
end
end

怎么了?

最佳答案

就像斯沃兹说的,你在一个类上调用了一个实例方法。重命名:

def self.events_and_repeats(date)

我只是在回答中写这个,因为评论太长了,
查看 ice-cube github 页面,它严格地说:
Include IceCube inside and at the top of your ActiveRecord model file to use the IceCube classes easily.

我也认为你不需要 require在你的模型中。

关于ruby-on-rails - 将方法添加到 Rails 中的模型时出现未定义的方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15799930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com