【问题标题】:Rails: Defining methods for my controllersRails:为我的控制器定义方法
【发布时间】:2013-05-25 04:25:50
【问题描述】:

新手问题!我试图弄清楚如何设置特定的 Active Record 搜索,然后从控制器调用这些搜索。为什么会抛出未定义的方法错误?

模型中定义的方法:

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

控制器

def index
    @events = Event.next_seven_days     
end

【问题讨论】:

    标签: ruby-on-rails methods model controller


    【解决方案1】:

    因为你还没有将你调用的方法定义为类方法

    试试

     def self.next_seven_days
    

    在 activerecord 类中,而不是你所拥有的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2012-07-18
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多