jedi
jedi

Reputation: 2207

Rails custom routes with scopes

I have a scope 'games-posts' in my Post model that fetches posts that belong to a Topic with a title "Games" (Post belongs_to Topic). In my routes I have defined the route get 'games-posts', to: 'topics#games' and have the view where I show the list of those games posts. In each of the posts I have a link that takes me to that specific games post. Apart from that I have regular resources :posts routes that the generate standard urls:

`/posts`
`/posts/:id`
`/posts/:id/edit`

etc.

My problem is that when I click on a specific post on games-posts page right now it redirects me to /posts/1 (for example). It would be cool if it redirected me to /games-posts/1 instead of /posts/1 and similarily I would like to edit and destroy those posts from games-posts page. How can I accomplish this? Is it possible to define something like

resources :games-posts?

Upvotes: 0

Views: 267

Answers (1)

Sharjeel
Sharjeel

Reputation: 15798

The :controller option lets you explicitly specify a controller to use for the resource. For example:

resources :games_posts, controller: 'posts'

Now run rake routes and you will get following output to verify if it generated your required paths.

    games_posts GET    /games_posts(.:format)                                   posts#index
                POST   /games_posts(.:format)                                   posts#create
 new_games_post GET    /games_posts/new(.:format)                               posts#new
edit_games_post GET    /games_posts/:id/edit(.:format)                          posts#edit
     games_post GET    /games_posts/:id(.:format)                               posts#show
                PATCH  /games_posts/:id(.:format)                               posts#update
                PUT    /games_posts/:id(.:format)                               posts#update
                DELETE /games_posts/:id(.:format)                               posts#destroy

Upvotes: 2

Related Questions