colmtuite
colmtuite

Reputation: 4491

Rails Nested Resource Scoped to Controller

In my app, websites have many pages. I'm trying to setup my URLs to look like

example.com/websites/1/pagename

I want it so page names don't need to be globally unique. They just need to be unique within the website they belong to.

This is what my routes look like so far

resources :websites do
    resources :pages, :path => ''
end

UPDATE

I got it working by changing this line in the pages controller.

def show
    @page = Page.find_by(website_id: params[:website_id], id: params[:id])
end

However, then I updated that line to use Friendly ID...

def show
    @page = Page.friendly.find_by(website_id: params[:website_id], id: params[:id])
end

Now I get an error undefined method name for nil:NilClass because I have <% provide(:title, @page.name) %>

Upvotes: 0

Views: 65

Answers (1)

Duyet Nguyen
Duyet Nguyen

Reputation: 543

No, You don't need.

The rails g controller websites/pages to use with namespace.

Your URL: websites/1 the id = 1 is unique. and the pagename also unique for each website

=> websites/1/pagename is unique

It's fine for:

websites/1/page_about_author

and

websites/2/page_about_author

Upvotes: 1

Related Questions