Pergunta

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.

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top