Rails Routing (root :to => …)

后端 未结 6 1581
北恋
北恋 2021-01-31 02:03

I know how to set the routes root of my rails app to a controller and an action.

But how to add an id?

/pages/show/1 should be the root.

How

6条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 02:34

    Matthew's solution works, but I think it is more readable to fetch the object. For example, let's say you want to root to the Page#show action for the page with the name "landing". This is a bit more readable:

    root :to => "pages#show", :id => Page.find_by_name("landing").id
    

    From a performance perspective, this solution is worse because it requires an additional database query, but this solution is more readable if performance is not a high priority.

提交回复
热议问题