2012-06-17 52 views
2

Iam在我的應用中使用Devise + CanCan + Rolify作爲授權和認證解決方案。Rails應用程序:鏈接到Devise視圖路由到主頁。

管理員正在我的應用中從他們的儀表板執行所有用戶訪問管理。所以我試着從我的「admin_dashboard.index.html文件

伊夫內訪問所有的設計視圖鏈接創造了這個應用程序

這裏用戶控制器的admin_dashboard_index.html.erb文件

<table id="users_index_table" %> 
    <tr> 
<th>id</th> 
    <th>user name</th> 
<th>user email</th> 
<th>user role</th> 
    <th>reset password</th> 
<th></th> 
<th></th> 
<th></th> 

    </tr> 
<% @users.each do |user| %> 
    <tr> 
<td><%= user.name %></td> 
<td><%= user.email %></td> 
    <td><%= user.roles %></td> 
<th><%= link_to 'reset password', edit_user_password_path %></th> 
<td><%= link_to %></td> 
    <td><%= link_to 'Show', user %></td> 
    <td><%= link_to 'Edit', edit_user_path(user) %></td> 
    <td><%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %></td> 
    </tr> 
<% end %> 
</table> 
<%= link_to 'New User', new_user_registration_path %> 

現在的問題是,當我在這些「用戶」鏈接點擊除「new_user_registration_path」所有的工作當我點擊這個它使我的應用程序的主頁和軌道服務器顯示此跟蹤:

Started GET "https://stackoverflow.com/users/sign_up" for 127.0.0.1 at 2012-06-16 18:24:49 -0700 
Processing by Devise::RegistrationsController#new as HTML 
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
Redirected to http://localhost:3000/ 
Filter chain halted as :require_no_authentication rendered or redirected 
Completed 302 Found in 2ms (ActiveRecord: 0.5ms) 

如何獲取新用戶鏈接,並使用「edit_user_password_path」工作,並將我路由到相應的字段而不是主頁。

感謝

回答

4

基於服務器輸出,它看起來像有一個過濾器,不會允許你創建一個新用戶,而你已經登錄(:require_no_authentication)。在研究如何設置設計之後,他們的註冊控制器上會有一個before過濾器,需要未經身份驗證的會話才能創建新的用戶註冊。

prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ] 

如果您的用戶控制器從設計:: RegistrationsController繼承,那麼你可以跳過完全過濾器:

skip_before_filter :require_no_authentication, :only => [:new, :create] 

或者你可以寫自己:require_no_authentication的方法來檢查,如果登錄用戶是管理員還是普通用戶。你可以在你的用戶控制器底部把這個方法:

protected 

def require_no_authentication 
    if current_user.is_admin? 
     return true 
    else 
     return super 
    end 
end 

如果您的控制器未從繼承設計:: RegistrationsController那麼你會設置你的路由時,需要指定您的自定義控制器。

devise_for :users, :controllers => { :registration => "my/controller" } 

然後明顯地實現你自己的'新'方法。

我發現了一些現有的堆棧溢出問題,這也涉及到這個問題:

rails:3 Devise signup Filter chain halted as :require_no_authentication rendered or redirected

Setup devise with custom registration controller

+0

嘿,非常感謝你的幫助:) – banditKing

+0

沒問題,很高興做到這一點! – awbergs

4

我重寫的註冊控制器和註釋sign_up線,當我創建的用戶,而其他用戶已經登錄

class RegistrationsController < Devise::RegistrationsController    
    skip_before_filter :require_no_authentication        
    def create           

    build_resource 

    if resource.save 
     if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_navigational_format? 
     # sign_up(resource_name, resource) 
     respond_with resource, :location => after_sign_up_path_for(resource) 
     else 
     set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? 
     expire_session_data_after_sign_in! 
     respond_with resource, :location => after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords resource 
     respond_with resource 
    end 
    end 
end 

和改變路線

devise_for :users, :controllers => { :registrations => "registrations" } 
相關問題