简体   繁体   English

Rails:定义控制器的方法

[英]Rails: Defining methods for my controllers

Newbie question! 新手问题! I'm trying to get my head around how I can set up specific Active Record searches which I then call from my controllers. 我试图弄清楚如何设置特定的Active Record搜索,然后从控制器调用它。 Why is this throwing an undefined method error? 为什么这会引发未定义的方法错误?

method defined in the Model: 模型中定义的方法:

class Event < ActiveRecord::Base

    attr_accessible :category, :date_end, :date_start, :description, :time_start, :title, :venue, :image


    def next_seven_days
        t = Time.now
        Event.where("date_end BETWEEN ? AND ?", t, t+7.days).order("date_end ASC")
    end
end

Controller 调节器

def index
    @events = Event.next_seven_days     
end

because you haven't defined the method you're calling as a class method 因为您尚未将要调用的方法定义为类方法

try 尝试

 def self.next_seven_days

in the activerecord class instead of what you have 在activerecord类中,而不是您拥有的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM