Question

I'm trying to create a url that looks like this:

   www.example.com/something/:a_slug

So a url that is /something where something is not a controller (but it is mapped to a controller, like a controller alias). Something is a fixed word.

Then :a_slug can be any slug passed into the a controller and picked up on an action.

Sort of a very custom show resource url.

It seems simple but I can't seem to get the route right.

Cheers!

Was it helpful?

Solution

routes.rb

match 'something/:a_slug' => 'widgets#show'

app/controllers/widgets_controller.rb

class WidgetsController < ApplicationController
  def show
    render :text => params[:a_slug]
  end 
end

And as if by magic, GET /something/feh shows feh in the browser.

Oh, of course this is for rails 3. It's similar for rails 2 but you didn't specify a requirement there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top