2015-01-13 140 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 

和那麼我只需在表單和導航欄鏈接中相應地調整我的路徑。謝謝大家!