Rails 控制器范围内的嵌套资源
Rails Nested Resource Scoped to Controller
在我的应用程序中,网站有很多页面。我正在尝试将我的网址设置为
example.com/websites/1/pagename
我想要它,这样页面名称就不必是全局唯一的。他们只需要在他们所属的网站内是独一无二的。
到目前为止我的路线是这样的
resources :websites do
resources :pages, :path => ''
end
更新
我通过更改页面控制器中的这一行使其正常工作。
def show
@page = Page.find_by(website_id: params[:website_id], id: params[:id])
end
但是,我更新了该行以使用 Friendly ID...
def show
@page = Page.friendly.find_by(website_id: params[:website_id], id: params[:id])
end
现在我得到一个错误 undefined method name for nil:NilClass
因为我有 <% provide(:title, @page.name) %>
不,你不需要。
与命名空间一起使用的rails g controller websites/pages
。
您的 URL:websites/1
id = 1
是独一无二的。 pagename
每个网站也是独一无二的
=> websites/1/pagename
是独一无二的
适合:
websites/1/page_about_author
和
websites/2/page_about_author
在我的应用程序中,网站有很多页面。我正在尝试将我的网址设置为
example.com/websites/1/pagename
我想要它,这样页面名称就不必是全局唯一的。他们只需要在他们所属的网站内是独一无二的。
到目前为止我的路线是这样的
resources :websites do
resources :pages, :path => ''
end
更新
我通过更改页面控制器中的这一行使其正常工作。
def show
@page = Page.find_by(website_id: params[:website_id], id: params[:id])
end
但是,我更新了该行以使用 Friendly ID...
def show
@page = Page.friendly.find_by(website_id: params[:website_id], id: params[:id])
end
现在我得到一个错误 undefined method name for nil:NilClass
因为我有 <% provide(:title, @page.name) %>
不,你不需要。
与命名空间一起使用的rails g controller websites/pages
。
您的 URL:websites/1
id = 1
是独一无二的。 pagename
每个网站也是独一无二的
=> websites/1/pagename
是独一无二的
适合:
websites/1/page_about_author
和
websites/2/page_about_author