简体   繁体   中英

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:

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

and then you can go with fl00r 's soulution:

@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.

Try this

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

Model

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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