:title rather than :order => title-6ren"> :title rather than :order => title-在查看 haml 文件中我有一个链接,当您单击时,您会看到所有按标题或发布日期排序的电影。 Controller 中的代码是: def index sort = params[:sort] |-6ren">
gpt4 book ai didi

ruby-on-rails - 当使用 "find_all_by"时,为什么使用 :order => :title rather than :order => title

转载 作者:太空宇宙 更新时间:2023-11-03 17:02:41 25 4
gpt4 key购买 nike

在查看 haml 文件中我有一个链接,当您单击时,您会看到所有按标题或发布日期排序的电影。 Controller 中的代码是:

def index
sort = params[:sort] || session[:sort]
case sort
when 'title'
ordering,@title_header = {:order => :title}, 'hilite'
when 'release_date'
ordering,@date_header = {:order => :release_date}, 'hilite'
end
# some more codes here
@movies = Movie.find_all_by_rating(@selected_ratings.keys, ordering)
end

如果我改变

ordering,@title_header = {:order => :title}, 'hilite'

 ordering,@title_header = {:order => title}, 'hilite'

报错:

undefined local variable or method `title' for #<MoviesController:0xb29a853c>

最佳答案

{:order => :title}

{:order => 'title'}

两者都有效。查看此线程以了解字符串和符号之间的差异:What's the difference between a string and a symbol in Ruby?

{:order => title}

不起作用,因为您试图为您的散列键分配一个包含在 title 局部变量中的值,但它不存在,因此它崩溃了

关于ruby-on-rails - 当使用 "find_all_by"时,为什么使用 :order => :title rather than :order => title,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13160773/

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