Neil Hoff
Neil Hoff

Reputation: 2075

Change routing of new and edit

I setup scaffolding for "posts" and would like to use ActiveAdmin to edit and create new.

In my routes.rb file I have posts setup like this:

resources :posts

I would like when someone goes to '/posts/new' to be redirected to '/admin/posts/new' The same thing with edit. From '/posts/:id/edit' to '/admin/posts/:id/edit'

How can I keep the other routes the same while changing the new and edit?

Edit

Here is my "rake routes"

 tags GET        /tags(.:format)                           tags#index
                           POST       /tags(.:format)                           tags#create
                   new_tag GET        /tags/new(.:format)                       tags#new
                  edit_tag GET        /tags/:id/edit(.:format)                  tags#edit
                       tag GET        /tags/:id(.:format)                       tags#show
                           PUT        /tags/:id(.:format)                       tags#update
                           DELETE     /tags/:id(.:format)                       tags#destroy
                admin_root            /admin(.:format)                          admin/dashboard#index
batch_action_admin_admin_users POST       /admin/admin_users/batch_action(.:format) admin/admin_users#batch_action
         admin_admin_users GET        /admin/admin_users(.:format)              admin/admin_users#index
                           POST       /admin/admin_users(.:format)              admin/admin_users#create
      new_admin_admin_user GET        /admin/admin_users/new(.:format)          admin/admin_users#new
     edit_admin_admin_user GET        /admin/admin_users/:id/edit(.:format)     admin/admin_users#edit
          admin_admin_user GET        /admin/admin_users/:id(.:format)          admin/admin_users#show
                           PUT        /admin/admin_users/:id(.:format)          admin/admin_users#update
                           DELETE     /admin/admin_users/:id(.:format)          admin/admin_users#destroy
           admin_dashboard            /admin/dashboard(.:format)                admin/dashboard#index
  batch_action_admin_posts POST       /admin/posts/batch_action(.:format)       admin/posts#batch_action
               admin_posts GET        /admin/posts(.:format)                    admin/posts#index
                           POST       /admin/posts(.:format)                    admin/posts#create
            new_admin_post GET        /admin/posts/new(.:format)                admin/posts#new
           edit_admin_post GET        /admin/posts/:id/edit(.:format)           admin/posts#edit
                admin_post GET        /admin/posts/:id(.:format)                admin/posts#show
                           PUT        /admin/posts/:id(.:format)                admin/posts#update
                           DELETE     /admin/posts/:id(.:format)                admin/posts#destroy
   batch_action_admin_tags POST       /admin/tags/batch_action(.:format)        admin/tags#batch_action
                admin_tags GET        /admin/tags(.:format)                     admin/tags#index
                           POST       /admin/tags(.:format)                     admin/tags#create
             new_admin_tag GET        /admin/tags/new(.:format)                 admin/tags#new
            edit_admin_tag GET        /admin/tags/:id/edit(.:format)            admin/tags#edit
                 admin_tag GET        /admin/tags/:id(.:format)                 admin/tags#show
                           PUT        /admin/tags/:id(.:format)                 admin/tags#update
                           DELETE     /admin/tags/:id(.:format)                 admin/tags#destroy
batch_action_admin_comments POST       /admin/comments/batch_action(.:format)    admin/comments#batch_action
            admin_comments GET        /admin/comments(.:format)                 admin/comments#index
                           POST       /admin/comments(.:format)                 admin/comments#create
             admin_comment GET        /admin/comments/:id(.:format)             admin/comments#show
    new_admin_user_session GET        /admin/login(.:format)                    active_admin/devise/sessions#new
        admin_user_session POST       /admin/login(.:format)                    active_admin/devise/sessions#create
destroy_admin_user_session DELETE|GET /admin/logout(.:format)                   active_admin/devise/sessions#destroy
       admin_user_password POST       /admin/password(.:format)                 active_admin/devise/passwords#create
   new_admin_user_password GET        /admin/password/new(.:format)             active_admin/devise/passwords#new
  edit_admin_user_password GET        /admin/password/edit(.:format)            active_admin/devise/passwords#edit
                           PUT        /admin/password(.:format)                 active_admin/devise/passwords#update
                     posts GET        /posts(.:format)                          posts#index
                           POST       /posts(.:format)                          posts#create
                  new_post GET        /posts/new(.:format)                      posts#new
                 edit_post GET        /posts/:id/edit(.:format)                 posts#edit
                      post GET        /posts/:id(.:format)                      posts#show
                           PUT        /posts/:id(.:format)                      posts#update
                           DELETE     /posts/:id(.:format)                      posts#destroy
                      root            /                                         posts#index

Upvotes: 0

Views: 1514

Answers (3)

Nick Kugaevsky
Nick Kugaevsky

Reputation: 2945

Try something like this

# config/routes.rb
resources :posts, except: [:new, edit] do
  get 'new' => redirect("/admin/posts/new"), :on => :collection
  get 'edit' => redirect("/admin/posts/:id/edit"), :on => :member
end

PS Please, add your $ rake routes to your question to make my code example clearer.

Upvotes: 1

Uģis Ozols
Uģis Ozols

Reputation: 1264

get "/posts/new" => redirect("/admin/posts/new")
get "/posts/:id/edit" => redirect("/admin/posts/:id/edit")
resources :posts

Upvotes: 0

Иван Бишевац
Иван Бишевац

Reputation: 14641

Maybe you could redirect_to inside edit and new methods.

Upvotes: 0

Related Questions