【问题标题】:Why do I get an error when trying to use a method in application_controller?尝试使用 application_controller 中的方法时为什么会出错?
【发布时间】:2015-04-21 11:16:39
【问题描述】:

在我的 Ruby on Rails 应用程序中,我有一个影院应用程序,并试图用显示日期不是过去的电影填充下拉菜单。

在我的 _form.html.erb 中,我有一个尝试使用 live_films 方法的下拉菜单:

<%= f.grouped_collection_select :showing_id, Film.order(:title).live_films, :showings, :title, :id,  :showing_times %>

方法在application_controller中:

helper_method :active_menu, :live_films 

def live_films 
    Film.includes(:showings).where.not(showings.show_date < Date.today)
end

我也怀疑我的方法行不通,所以我欢迎有关让它发挥作用的建议。

我做错了什么?

【问题讨论】:

标签: ruby-on-rails ruby class methods controller


【解决方案1】:

您应该在 live_films 方法调用的结果上调用 order

<%= f.grouped_collection_select :showing_id, live_films.order(:title), :showings, :title, :id,  :showing_times %>

【讨论】:

  • 谢谢!这行得通!您建议如何整理该方法,使其不显示过去的日期,showings.show_date &lt; Date.today 不起作用。
  • 试试:Film.includes(:showings).where('showings.show_date &gt; ?', Date.current.beginning_of_day)。请注意,使用 Date.today 返回系统日期而不考虑 Rails 时区。使用Date.current 来识别日期时区。
  • 导致此错误:SQLite3::SQLException: no such column: showings.show_date: SELECT "films".* FROM "films" WHERE (showings.show_date &gt; '2015-02-19 00:00:00.000000') ORDER BY "films"."title" ASC
  • @benjs.1,错误解决了吗?为show_date 使用适当的列名,我从您的问题中复制了表名和列名。
  • 不,我仍然有错误,那是正确的表和列名
猜你喜欢
  • 2018-12-12
  • 1970-01-01
  • 2021-10-08
  • 2013-03-07
  • 1970-01-01
  • 2023-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多