开发者

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 app开发者_如何学编程reciated.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜