2015-01-13 105 views
0

我对Ruby/Rails中的应用程序有一个问题路由。我有两种不同的模型,一个用户和一个管理员。首先,我设置了用户,并独立运行。用户有“sign_in”,“signed_out”,“account_settings”,“登录”方法等。Rails 4路由

然后我创建了一个管理模型。这与用户非常相似,除了它有能力删除/禁止用户模型。问题是,我想使用类似的路由方法,除了前缀“/ admin /”。我正在研究范围和命名空间,但我无法正确地路由所有内容。这就是我的config/routes.rb文件看起来像一个时刻:

Rails.application.routes.draw do 

    root 'home#index' 

    scope 'admin' do 
    get "sign_in" => "admin#sign_in" 
    post "sign_in" => "admin#login" 
    get "signed_out" => "admin#signed_out" 
    end 

    get "sign_in" => "authentication#sign_in" 
    post "sign_in" => "authentication#login" 

    get "signed_out" => "authentication#signed_out" 
    get "new_user" => "authentication#new_user" 
    put "new_user" => "authentication#register" 

    get "account_settings" => "authentication#account_settings" 
    put "account_settings" => "authentication#set_account_info" 

    get "forgot_password" => "authentication#forgot_password" 
    put "forgot_password" => "authentication#send_password_reset_instructions" 

    get "password_reset" => "authentication#password_reset" 
    put "password_reset" => "authentication#new_password" 
end 

有什么,我做错了什么?我要为管理员航线是localhost:3000/admin/sign_inlocalhost:3000/admin/signed_out和路线对于用户而言,localhost:3000/sign_inlocalhost:3000/signed_out

感谢

增加:的rake routes输出如下

$ rake routes 
      Prefix Verb URI Pattern      Controller#Action 
      root GET/        home#index 
     sign_in GET /admin/sign_in(.:format)   admin#sign_in 
       POST /admin/sign_in(.:format)   admin#login 
     signed_out GET /admin/signed_out(.:format)  admin#signed_out 
account_settings GET /admin/account_settings(.:format) admin#account_settings 
       GET /sign_in(.:format)    authentication#sign_in 
       POST /sign_in(.:format)    authentication#login 
       GET /signed_out(.:format)    authentication#signed_out 
     new_user GET /new_user(.:format)    authentication#new_user 
       PUT /new_user(.:format)    authentication#register 
       GET /account_settings(.:format)  authentication#account_settings 
       PUT /account_settings(.:format)  authentication#set_account_info 
forgot_password GET /forgot_password(.:format)  authentication#forgot_password 
       PUT /forgot_password(.:format)  authentication#send_password_reset_instructions 
    password_reset GET /password_reset(.:format)   authentication#password_reset 
       PUT /password_reset(.:format)   authentication#new_password 

我怎样才能创建自定义帮助程序路径,以便我不需要在每个表单/链接中手动输入它们。

编辑:

新航线信息文件如下

root_path GET/ home#index 
admin_sign_in_path GET /admin/sign_in(.:format) admin/admin#sign_in 
POST /admin/sign_in(.:format) admin/admin#login 
admin_signed_out_path GET /admin/signed_out(.:format) admin/admin#signed_out 
admin_account_settings_path GET /admin/account_settings(.:format) admin/admin#account_settings 
sign_in_path GET /sign_in(.:format) authentication#sign_in 
POST /sign_in(.:format) authentication#login 
signed_out_path GET /signed_out(.:format) authentication#signed_out 
new_user_path GET /new_user(.:format) authentication#new_user 
PUT /new_user(.:format) authentication#register 
account_settings_path GET /account_settings(.:format) authentication#account_settings 
PUT /account_settings(.:format) authentication#set_account_info 
forgot_password_path GET /forgot_password(.:format) authentication#forgot_password 
PUT /forgot_password(.:format) authentication#send_password_reset_instructions 
password_reset_path GET /password_reset(.:format) authentication#password_reset 
PUT /password_reset(.:format) authentication#new_password 
+0

现在它甚至发送'localhost:3000/sign_in'到管理员登录页面 – Josh

+0

我不确定,但是您是否尝试将其更改为'scope'/ admin''? – DanneManne

+0

在设计路线时使用“$ rake routes” –

回答

0

最初的方法是行不通的,因为它是简单的“sign_in_path”重新绑定到管理控制器,然后也没有帮手认证控制器的路径,尽管路由设置正确,帮助器在表单和链接指向“不正确的事情”的指针。

要解决,我访问了localhost:3000/rails/info/routes,并注意到,这些帮工如下:

$ rake routes 
      Prefix Verb URI Pattern      Controller#Action 
      root GET/        home#index 
     sign_in GET /admin/sign_in(.:format)   admin#sign_in 
       POST /admin/sign_in(.:format)   admin#login 
     signed_out GET /admin/signed_out(.:format)  admin#signed_out 
account_settings GET /admin/account_settings(.:format) admin#account_settings 
       GET /sign_in(.:format)    authentication#sign_in 
       POST /sign_in(.:format)    authentication#login 
       GET /signed_out(.:format)    authentication#signed_out 
     new_user GET /new_user(.:format)    authentication#new_user 
       PUT /new_user(.:format)    authentication#register 
       GET /account_settings(.:format)  authentication#account_settings 
       PUT /account_settings(.:format)  authentication#set_account_info 
forgot_password GET /forgot_password(.:format)  authentication#forgot_password 
       PUT /forgot_password(.:format)  authentication#send_password_reset_instructions 
    password_reset GET /password_reset(.:format)   authentication#password_reset 
       PUT /password_reset(.:format)   authentication#new_password 

我固定我config/routes.rb文件包含以下内容:

Rails.application.routes.draw do 

    root 'home#index' 

    namespace :admin, :module => false do 
    get "sign_in" => "admin#sign_in" 
    post "sign_in" => "admin#login" 
    get "signed_out" => "admin#signed_out" 
    get "account_settings" => "admin#account_settings" 
    put "account_settings" => "admin#set_account_info" 
    get "users" => "admin#users" 
    delete "user/:id" => "admin#delete_user", :as =>"user" 
    end 

    get "sign_in" => "authentication#sign_in" 
    post "sign_in" => "authentication#login" 

    get "signed_out" => "authentication#signed_out" 
    get "new_user" => "authentication#new_user" 
    put "new_user" => "authentication#register" 

    get "account_settings" => "authentication#account_settings" 
    put "account_settings" => "authentication#set_account_info" 

    get "forgot_password" => "authentication#forgot_password" 
    put "forgot_password" => "authentication#send_password_reset_instructions" 

    get "password_reset" => "authentication#password_reset" 
    put "password_reset" => "authentication#new_password" 
end 

,然后让我的助手路径看更像如下:

root_path GET/ home#index 
admin_sign_in_path GET /admin/sign_in(.:format) admin#sign_in 
POST /admin/sign_in(.:format) admin#login 
admin_signed_out_path GET /admin/signed_out(.:format) admin#signed_out 
admin_account_settings_path GET /admin/account_settings(.:format) admin#account_settings 
PUT /admin/account_settings(.:format) admin#set_account_info 
admin_users_path GET /admin/users(.:format) admin#users 
admin_user_path DELETE /admin/user/:id(.:format) admin#delete_user 
sign_in_path GET /sign_in(.:format) authentication#sign_in 
POST /sign_in(.:format) authentication#login 
signed_out_path GET /signed_out(.:format) authentication#signed_out 
new_user_path GET /new_user(.:format) authentication#new_user 
PUT /new_user(.:format) authentication#register 
account_settings_path GET /account_settings(.:format) authentication#account_settings 
PUT /account_settings(.:format) authentication#set_account_info 
forgot_password_path GET /forgot_password(.:format) authentication#forgot_password 
PUT /forgot_password(.:format) authentication#send_password_reset_instructions 
password_reset_path GET /password_reset(.:format) authentication#password_reset 
PUT /password_reset(.:format) authentication#new_password 

和那么我只需在表单和导航栏链接中相应地调整我的路径。谢谢大家!