dynamic - Rails routes: same controller different routes -
in our app have tag
model, such red
yellow
big
small
etc. in several categories color
, size
(tag model have field category
). now, instead of
/tags/big /tags/small /tags/red /tags/yellow /tags
we want routes like
/size/big /size/small /color/red /color/yellow /size /color ...
also, url helpers should work too, i.e., tag_path
should yield correct url's how in routes.rb file? thanks!!
edit:
- need index pages/url_helper work too
- will_paginate uses tag_path generate links page numbers. need overload tag_path?
you can use route constraints. define class executes matches customized conditions. can search api match
see details.
i assume tag has 2 columns named "category" , "name".
route.rb
match ':category/:name' => 'tags#show', via: 'get', constraints: tagroutewhitelist.new, as: 'tag'
app/models/tag_route_white_list.rb
class tagroutewhitelist def matches(request) tag.distinct('category').include?(request.params[:category]) && tag.distinct('name').include?(request.params[:name]) end end
in view, can use tag_path(category: @tag.category, name: @tag.name)
Comments
Post a Comment