elkalto23
elkalto23

Reputation: 267

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?

Upvotes: 3

Views: 2088

Answers (2)

BitOfUniverse
BitOfUniverse

Reputation: 6011

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.

Upvotes: 7

fl00r
fl00r

Reputation: 83680

Try this

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

Model

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

Upvotes: 3

Related Questions