我正在尝试将 Rails 主页映射到资源生成的页面,但在将所需参数添加到 route 时遇到了一些困难。我正在寻找类似的东西:
root :to => "pages#show", :slug => 'home'
...这不起作用。任何建议表示赞赏。
最佳答案
您可以在 Pages SHOW 操作中设置默认值,这样,如果您请求不带参数的 Pages#show,您将加载主页,而不是“未找到记录”。 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
然后您现有的设置:root :to => "pages#show"
应该按预期工作。
关于routes - 轨道 3 : Routing to a resource#show for root,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4572991/