Starkers
Starkers

Reputation: 10551

Create routes for an admin area

This is causing me such grief!

Let's say I have a story class.

A normal user can create a story, view their stories and delete a story belonging to them.

An admin user can create a story, view all stories and delete all stories.

I want an admin area in the url structure:

/control_panel/stories # list all stories in the site
/control_panel/stories/new # create a new story
/control_panel/stories/:id # show, edit, update, delete story (via different request methods)

However, I also want users to have these routes:

/stories
/stories/new
/stories/:id

I have no idea how to implement this. Trying to create the routes has been a nightmare. This configuration:

  resources :stories
  scope '/control_panel' do
    resources :stories

  end

Is almost there:

   stories GET    /stories(.:format)                        stories#index
           POST   /stories(.:format)                        stories#create
 new_story GET    /stories/new(.:format)                    stories#new
edit_story GET    /stories/:id/edit(.:format)               stories#edit
     story GET    /stories/:id(.:format)                    stories#show
           PATCH  /stories/:id(.:format)                    stories#update
           PUT    /stories/:id(.:format)                    stories#update
           DELETE /stories/:id(.:format)                    stories#destroy
           GET    /control_panel/stories(.:format)          stories#index
           POST   /control_panel/stories(.:format)          stories#create
           GET    /control_panel/stories/new(.:format)      stories#new
           GET    /control_panel/stories/:id/edit(.:format) stories#edit
           GET    /control_panel/stories/:id(.:format)      stories#show
           PATCH  /control_panel/stories/:id(.:format)      stories#update
           PUT    /control_panel/stories/:id(.:format)      stories#update
           DELETE /control_panel/stories/:id(.:format)      stories#destroy

However, where are my named routes for the control_panel routes?! I expected:

GET /control_panel/stories to have the name control_panel_stories (like its corresponding non-namespaced routes /stories),

GET /control_panel/stories/new to have the name new_control_panel_story

GET /control_panel/stories/:id/edit to have the name edit_control_panel_story

and

GET /control_panel/stories/:id to have the name control_panel_story

Instead I have no named routes!

Questions:


resources :stories
namespace :control_panel do
    resources :stories
end

gives me:

   control_panel_stories GET    /control_panel/stories(.:format)          control_panel/stories#index
                         POST   /control_panel/stories(.:format)          control_panel/stories#create
 new_control_panel_story GET    /control_panel/stories/new(.:format)      control_panel/stories#new
edit_control_panel_story GET    /control_panel/stories/:id/edit(.:format) control_panel/stories#edit
     control_panel_story GET    /control_panel/stories/:id(.:format)      control_panel/stories#show
                         PATCH  /control_panel/stories/:id(.:format)      control_panel/stories#update
                         PUT    /control_panel/stories/:id(.:format)      control_panel/stories#update
                         DELETE /control_panel/stories/:id(.:format)      control_panel/stories#destroy

Upvotes: 0

Views: 68

Answers (2)

sites
sites

Reputation: 21815

Try with:

scope :path => 'control_panel', :as => 'control_panel' do

For example you want the admin to be able to destroy, but not a regular user:

def destroy
  return redirect_to(:back, :notice => 'You need to be admin to delete.') if current_user.regular?
  ...
end

Upvotes: 1

Raymond Liu
Raymond Liu

Reputation: 56

about routes sope and namespace, you should have a look at this article

Upvotes: 1

Related Questions