I am trying to generate routes in my rails app. The app has Movie table and column genre. I want to make the routes like this: /movies-genre-horror and shows all the movies with genre horror. Is it actually possible? If yes, can anyone give a hint how to do it? I would be nice if there's tutorial to do it. I did make like this: /movies/genre/horror and it works. I use gem friendly_id and use slug to change the genre that not url friendly.
Asked
Active
Viewed 1,143 times
0
-
you could generate a fallback route, where you look inside the controller, if you get some matches in your database. But why changing default behaviour? I think it looks nice `/movies/genre/horror` – 23tux May 14 '13 at 08:54
-
would you mine to elaborate? how to generate a fallback routes? Thank you – ishwr May 14 '13 at 09:05
1 Answers
0
Add a default route to your routes.rb
. At the very end just do a
match ":default" => "foo#default"
And inside foo_controller.rb
def default
# here are your params
parts = params[:default].split("-")
controller = parts[0]
action = parts[1]
genre = parts[2]
records = Movie.where(genre: genre)
if records.count > 0
# redirect to your controller aciton
else
raise ActionController::RoutingError.new('Not Found')
end
end
It's not tested, but I think you should get the point.

23tux
- 14,104
- 15
- 88
- 187