0

I have a route get '/catalog/:gender/' => 'catalog#gender'. The :gender param is used in my controller like so @products = Product.where(gender: params[:gender]).all.

So I can use /catalog/male/ to get all male products and /catalog/female/ to get all female products. The question is, is there a way somebody could possibly pass both male and female parameters and get all the products?

Viktor
  • 4,218
  • 4
  • 32
  • 63

1 Answers1

1

You can pass /catalog/all/ to get all products, and in your controller:

if params[:gender] == "all"
  @products = Product.all
else
  @products = Product.where(gender: params[:gender])
end

Or render gender params optional get '/catalog(/:gender)' and then in your controller:

if params[:gender]
  @products = Product.where(gender: params[:gender])
else
  @products = Product.all
end

To complete answer, Query string can resolve your issue too.

Let's create a route /catalog/, and submit an array like /catalog?gender[]=male&gender[]=female.

# params[:gender] = ["male", "female"]
# SELECT "catalogs".* FROM "catalogs" WHERE "catalogs"."gender" IN ('male', 'female')
@products = Catalog.where(gender: params[:gender])

Answers to complete this solution:

Community
  • 1
  • 1
Florent Ferry
  • 1,397
  • 1
  • 10
  • 21
  • Thank you. But I was wondering if somebody could possibly pass few parameters to the link and get few categories of the product. I guess the answer is "No". – Viktor Jun 12 '16 at 08:27