Question

How do a route like this?

www.site.com/clothing/men/T-Shirts_type/Nike_brand/100-500_price/Red,White,Blue_color/
www.site.com/clothing/woman/Nike_brand/100-500_price/Red,White,Blue_color/

Should always be in order:

www.site.com/Sex/Type/Brand/Price/Color

Even if not insert all the available options:

www.site.com/Type/Color

The idenficador would always _something.

And the comma to enter more than one item.



EDIT 01

I need routes understand Value_something. And return like that:

param[:_something] = Values

Ex: 01 - One attribute

URL: site.com/clothing/men/T-Shirts_type
param[:_type] = T-Shirts

Ex: 02 - Two attributes

URL: site.com/clothing/men/T-Shirts_type/Nike_brand
param[:_type] = T-Shirts
param[:_brand] = Nike

Ex: 03 - Two attributes without order

URL: site.com/clothing/men/Nike_brand/T-Shirts_type
param[:_brand] = Nike
param[:_type] = T-Shirts

Ex: 04 - Multiple params in attribute

URL: site.com/clothing/men/Red,White,Blue_color
param[:_color] = Red,White,Blue

Ex: 05 - All attributes with order

URL: site.com/clothing/men/T-Shirts_type/Nike_brand/100-500_price/Red,White_color
param[:_type] = T-Shirts
param[:_brand] = Nike
param[:_price] = 100-500
param[:_color] = Red,White

Ex: 05 - All attributes without order

URL: site.com/clothing/men/Red,White_color/T-Shirts_type/100-500_price/Nike_brand
param[:_color] = Red,White
param[:_type] = T-Shirts
param[:_price] = 100-500
param[:_brand] = Nike
Was it helpful?

Solution

Make a custom route for each of the different cases. eg

#in config/routes.rb
get '/clothing/:sex/:option1/:option2/:option3/:option4/:option5', to: 'product#index'
get '/clothing/:sex/:option1/:option2/:option3/:option4', to: 'product#index'
get '/clothing/:sex/:option1/:option2/:option3', to: 'product#index'
get '/clothing/:sex/:option1/:option2', to: 'product#index'
get '/clothing/:sex/:option1', to: 'product#index'

Then in your index action you'll want to do something like

options = [params[:option1], params[:option2], params[:option3], params[:option4], params[:option5]].reject(&:blank?)
condition_strings = ["sex = #{params[:sex]}"]
options.each do |option_string|
  choices, category = option_string.split(" ")
  condition_strings << "#{category} in (#{choices})"
end
conditions = condition_strings.map{|string| "(#{string})"}.join(" AND ")
@products = Product.find(:all, :conditions => [conditions])

That said, i think this is a really horrible url schema. I would think it would be better to have all the different options as parameters rather than part of the path itself, eg have urls like

www.site.com/clothing?gender=men&type=T-Shirts&brand=Nike&price=100-500&color=Red,White,Blue

This is a much more conventional way of doing things.

EDIT - a rewrite of the above controller-side processing, to make the params structure you want:

options = [params[:option1], params[:option2], params[:option3], params[:option4], params[:option5]].reject(&:blank?)
options.each do |option_string|
  choices, category = option_string.split("_")
  params[category] = choices
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top