简体   繁体   English

将链式示波器作为第3栏上的参数传递?

[英]passing chained scopes as params on rails 3?

i have this on my view: 我有这个观点:

<%= link_to "shirts", things_path (:scope => "shirts" ) %>

this on my controller 这在我的控制器上

@products = Product.send(params[:scope])

however i would like to use chained scopes such as: 但是我想使用链式范围,例如:

<%= link_to "shirts", products_path (:scope => "shirts.blue" )  %>

but for some reason that doesnt work. 但由于某些原因,它不起作用。

Probably my syntax is wrong? 可能我的语法错了?

If you gonna use params as argument for send method, don't forget to check it by the whitelist of scopes: 如果你要使用params作为send方法的参数,不要忘记通过范围的白名单来检查它:

safe_scopes = %w(shirts pants boots blue red yellow)

and then you can go with fl00r 's soulution: 然后你可以使用fl00r的洗剂:

@products = Product.scoped
params[:scope].split(".").each{|scope| @products = @products.send(scope) if safe_scopes.include?(scope)}

because someone will try to send ?scope=shirts.destroy_all to your controller or smth else. 因为有人会尝试将?scope=shirts.destroy_all发送给您的控制器或其他人。

Try this 尝试这个

<%= link_to "shirts", products_path (:scope => "shirts.blue" ) %>

Model 模型

@products = Product.scoped
params[:scope].split(".").each{|scope| @products = @products.send(scope)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM