好的,我知道这是关于SaaS课程的内容,人们也在提出相关问题,但我已经花费了很多时间尝试和阅读,现在卡住了。首先,当您有一个名为Movie的模型时,使用Ratings作为模型并将它们关联起来是否更好,还是只是在空间中保留Ratings数组?其次,在我的控制器中,这是我现在拥有的:
def index
@movies = Movie.where(params[:ratings].present? ? {:rating => (params[:ratings].keys)} : {}).order(params[:sort])
@sort = params[:sort]
@ratings = Ratings.all
end
现在,我决定创建一个评分模型,因为我认为这会更好。以下是我的看法:
= form_tag movies_path, :method => :get do
Include:
- @ratings.each do |rating|
= rating.rating
= check_box_tag "ratings[#{rating.rating}]"
= submit_tag "Refresh"
我尝试了所有与在复选框标签中使用条件三元运算符有关的方法,以“ .include?(rating)? true:“”结尾。我尝试了所有应该有效的方法,但都不起作用。我不需要确切的答案,只需要指导。谢谢!
更新(控制器方法):
以下是控制器中的 index 方法,用于读取此哈希表 - 为了清晰起见。对于之前的模糊,我感到抱歉。
def index
@all_stores = Product.all_stores
@selected_stores = params[:stores] || session[:stores] || {}
if @selected_stores == {}
@selected_stores = Hash[@all_stores.map {|store| [store, store]}]
end
if params[:stores] != session[:stores]
# session[:stores] = @selected_stores
session[:stores] = params[:stores]
redirect_to :stores => @selected_stores and return
end
@products = Product.order("created_at desc").limit(150).find_all_by_store(@selected_stores.keys).group_by { |product| product.created_at.to_date}
. . . etc
RATINGS = ['R', 'PG', etc.]
。 - cdesrosiers