Rails namespaces and routing

房东的猫 提交于 2019-12-24 06:40:39

问题


I need help. I want administration for my rails application. I tried to set the routes with namespaces, but namespaces require a resource, and resource must have id in get request.

Anybody know how to set up correctly? I using windows machine. Thanks.

My routes :

Web::Application.routes.draw do

  namespace :admin do
    resources :access # GET http://localhost/admin/access/login/login - stupid??
  end

  match ':controller(/:action(/:id))(.:format)'
end

回答1:


Try to use resource :access instead of resources :access

  namespace :admin do
    resource :access
  end

It will generate routes:

         admin_access POST   /admin/access(.:format)                                admin/access#create
          new_admin_access GET    /admin/access/new(.:format)                            admin/access#new
         edit_admin_access GET    /admin/access/edit(.:format)                           admin/access#edit
                              GET    /admin/access(.:format)                                admin/access#show
                              PUT    /admin/access(.:format)                                admin/access#update
                              DELETE /admin/access(.:format)                                admin/access#destroy



回答2:


  namespace :admin do
    get "login" => "access#login", :as => :login # GET http://localhost/admin/login - admin_login_path 
  end



回答3:


If you don't have a set of restful resources, but just want a set of different controller methods, here's one way you can do it:

scope '/admin' do
  get '' => "admin#index", :as => 'admin_home'
  get '/users' => 'admin#users', :as => 'admin_users'
  get '/other_admin_task' => 'admin#other_admin_task', :as => 'other_admin_task'
end


来源:https://stackoverflow.com/questions/10686697/rails-namespaces-and-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!