我有以下代码用于控制器中的活动记录条件
@this_section = Section.all( :conditions => [ "url_section = ?", params[:ssection] ] )
当我尝试转到与之关联的网页时,我在浏览器中收到以下错误:
ArgumentError in UpcomingEventsController#index
wrong number of arguments (given 1, expected 0)
在美洲狮日志中,我得到:
Started GET "/" for 127.0.0.1 at 2017-11-21 11:06:22 +0000
Processing by UpcomingEventsController#index as HTML
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.0ms)
ArgumentError (wrong number of arguments (given 1, expected 0)):
app/controllers/upcoming_events_controller.rb:11:in `index'
11 是这篇文章顶部的代码行
我正在使用http://guides.rubyonrails.org/active_record_querying.html来弄清楚如何去做。
这个页面的路由是
get 'upcoming_events/:ssection/:cost', to: 'upcoming_events#index'
我想要做的是将@this_section 设置为数据库 WHERE url_section = params[:ssection] 的部分表中的记录。我正在使用导轨 5。
您正在尝试将 2.3 查询语法应用于 ActiveRecord 5.x。正确的方法是:
@this_section = Section.where(url_section: params[:section])
或者可能更准确(因为在这种情况下您似乎只需要一个实例):
@this_section = Section.find_by(url_section: params[:section])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句