[英]Rails 4 Routes Params Optionally
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
. Idenficador总是会
_something
。
And the comma to enter more than one item. 和逗号要输入多个项目。
EDIT 01 编辑01
I need routes understand Value_something. 我需要路线了解Value_something。 And return like that:
然后返回:
param[:_something] = Values
Ex: 01 - One attribute 例如:01-一个属性
URL: site.com/clothing/men/T-Shirts_type
param[:_type] = T-Shirts
Ex: 02 - Two attributes 例如:02-两个属性
URL: site.com/clothing/men/T-Shirts_type/Nike_brand
param[:_type] = T-Shirts
param[:_brand] = Nike
Ex: 03 - Two attributes without order 例如:03-两个属性没有顺序
URL: site.com/clothing/men/Nike_brand/T-Shirts_type
param[:_brand] = Nike
param[:_type] = T-Shirts
Ex: 04 - Multiple params in attribute 例如:04-属性中有多个参数
URL: site.com/clothing/men/Red,White,Blue_color
param[:_color] = Red,White,Blue
Ex: 05 - All attributes with order 例如:05-所有属性按顺序
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 例如:05-所有属性无顺序
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
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. 就是说,我认为这是一个非常可怕的URL模式。 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 www.site.com/clothing?gender=men&type=T-Shirts&brand=Nike&price=100-500&color=红色,白色,蓝色
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: 编辑-重写上面的控制器端处理,以创建所需的params结构:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.