PlankTon
PlankTon

Reputation: 12605

Rails 3: Routing to a resource#show for root

I'm trying to map my rails homepage to a page generated by a resource, and I'm having a little difficulty getting the required parameter into the route. I'm looking for something like:

root :to => "pages#show", :slug => 'home'

...which doesn't work. Any suggestions appreciated.

Upvotes: 2

Views: 639

Answers (2)

Andrew
Andrew

Reputation: 43123

You could put a default in your Pages SHOW action, so that instead of Record Not Found you'd load home if you were requesting Pages#show without a parameter. IE:

def show
  if
    # I'm guessing you're using slugs based on your question
    @page = Page.find_by_slug(params[:slug]) 
  else
    @page = Page.find_by_slug('home')
  end
  # This will automatically render 'show', but you could pass explicit render instructions too
end

Then your existing setup: root :to => "pages#show" should work as expected.

Upvotes: 2

sethvargo
sethvargo

Reputation: 26997

root :to => redirect('/pages')

Upvotes: 1

Related Questions