2011-07-05 74 views
2

我正在嘗試使用Devise進行兩步確認,如heroku。Rails&Devise:兩步確認路線錯誤

我的路線:

devise_for :user, :controllers => {:confirmations => "confirmations", :registrations => "registrations" } 
    put "confirm_account", :to => "confirmations#confirm_account" 

這裏是我的備用確認控制器:

class ConfirmationsController < Devise::ConfirmationsController 
    def show 
    @account = User.find_by_confirmation_token(params[:confirmation_token]) 
    if [email protected]? 
     render_with_scope :new 
    end 
    end 

    def confirm_account 
    @account = User.find(params[:account][:confirmation_token]) 
    if @account.update_attributes(params[:account]) and @account.password_match? 
     @account = User.confirm_by_token(@account.confirmation_token) 
     set_flash_message :notice, :confirmed  
     sign_in_and_redirect("user", @account) 
    else 
     render :action => "show" 
    end 
    end 

end 

這裏是我的show.html.erb

<%= form_for(resource, :as => resource_name, :url => confirm_account_path(resource_name)) do |f| %> 
    <%= f.label :email %> 
    <%= @account.email %> 
    <%= f.hidden_field :confirmation_token %> 
    <%= f.label :password %> 
    <%= f.password_field :password %> 
    <%= f.label :password_confirmation %> 
    <%= f.password_field :password_confirmation %> 
    <%= f.submit 'Confirm Account' %> 
    <%= link_to 'Home', root_url %> 
    <%= render :partial => 'devise/shared/links' %> 
<% end %> 

當我點擊confirm填寫密碼後(點擊後在確認郵件中確認)。我被路由到/confirm_account.user這很奇怪,對吧?發生什麼事情會導致這個問題?

編輯

rake routes回報:

 new_user_session GET /user/sign_in(.:format)     {:action=>"new", :controller=>"devise/sessions"} 
      user_session POST /user/sign_in(.:format)     {:action=>"create", :controller=>"devise/sessions"} 
    destroy_user_session GET /user/sign_out(.:format)     {:action=>"destroy", :controller=>"devise/sessions"} 
      user_password POST /user/password(.:format)     {:action=>"create", :controller=>"devise/passwords"} 
     new_user_password GET /user/password/new(.:format)    {:action=>"new", :controller=>"devise/passwords"} 
     edit_user_password GET /user/password/edit(.:format)    {:action=>"edit", :controller=>"devise/passwords"} 
         PUT /user/password(.:format)     {:action=>"update", :controller=>"devise/passwords"} 
cancel_user_registration GET /user/cancel(.:format)     {:action=>"cancel", :controller=>"registrations"} 
     user_registration POST /user(.:format)       {:action=>"create", :controller=>"registrations"} 
    new_user_registration GET /user/sign_up(.:format)     {:action=>"new", :controller=>"registrations"} 
    edit_user_registration GET /user/edit(.:format)      {:action=>"edit", :controller=>"registrations"} 
         PUT /user(.:format)       {:action=>"update", :controller=>"registrations"} 
         DELETE /user(.:format)       {:action=>"destroy", :controller=>"registrations"} 
     user_confirmation POST /user/confirmation(.:format)    {:action=>"create", :controller=>"confirmations"} 
    new_user_confirmation GET /user/confirmation/new(.:format)   {:action=>"new", :controller=>"confirmations"} 
         GET /user/confirmation(.:format)    {:action=>"show", :controller=>"confirmations"} 
      user_unlock POST /user/unlock(.:format)     {:action=>"create", :controller=>"devise/unlocks"} 
     new_user_unlock GET /user/unlock/new(.:format)    {:action=>"new", :controller=>"devise/unlocks"} 
         GET /user/unlock(.:format)     {:action=>"show", :controller=>"devise/unlocks"} 
     confirm_account PUT /confirm_account(.:format)    {:action=>"confirm_account", :controller=>"confirmations"} 
     editreject_admin GET /admin/:id/editreject(.:format)   {:action=>"editreject", :controller=>"admin"} 
      reject_admin GET /admin/:id/reject(.:format)    {:action=>"reject", :controller=>"admin"} 
      accept_admin GET /admin/:id/accept(.:format)    {:action=>"accept", :controller=>"admin"} 
    entries_admin_index GET /admin/entries(.:format)     {:action=>"entries", :controller=>"admin"} 
preferences_admin_index GET /admin/preferences(.:format)    {:action=>"preferences", :controller=>"admin"} 
      admin_index GET /admin(.:format)       {:action=>"index", :controller=>"admin"} 
      about_entries GET /entries/about(.:format)     {:action=>"about", :controller=>"entries"} 
      all_entries GET /entries/all(.:format)     {:action=>"all", :controller=>"entries"} 
     myentries_entries GET /entries/myentries(.:format)    {:action=>"myentries", :controller=>"entries"} 
       rate_entry GET /entries/:id/rate(.:format)    {:action=>"rate", :controller=>"entries"} 
      submit_entry PUT /entries/:id/submit(.:format)    {:action=>"submit", :controller=>"entries"} 
      entry_comments POST /entries/:entry_id/comments(.:format)  {:action=>"create", :controller=>"comments"} 
      entry_comment DELETE /entries/:entry_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"} 
       entries GET /entries(.:format)      {:action=>"index", :controller=>"entries"} 
         POST /entries(.:format)      {:action=>"create", :controller=>"entries"} 
       new_entry GET /entries/new(.:format)     {:action=>"new", :controller=>"entries"} 
       edit_entry GET /entries/:id/edit(.:format)    {:action=>"edit", :controller=>"entries"} 
        entry GET /entries/:id(.:format)     {:action=>"show", :controller=>"entries"} 
         PUT /entries/:id(.:format)     {:action=>"update", :controller=>"entries"} 
         DELETE /entries/:id(.:format)     {:action=>"destroy", :controller=>"entries"} 
           /auth/:service/callback(.:format)   {:controller=>"services", :action=>"create"} 
       services GET /services(.:format)      {:action=>"index", :controller=>"services"} 
         POST /services(.:format)      {:action=>"create", :controller=>"services"} 
        root  /(.:format)        {:controller=>"entries", :action=>"index"} 
       offline  /offline(.:format)      {:controller=>"application", :action=>"offline"} 

編輯3 在改變

devise_for :user, :controllers => {:confirmations => "confirmations", :registrations => "registrations" } do 
     match "/confirm_account", :to => "confirmations#confirm_account" 
    end 

我收到:

你沒有對象,當你沒有 期待它!您可能預期了Array的一個 實例。在評估零發生 錯誤。[]

{"utf8"=>"âœ「", 
"authenticity_token"=>"dsG/e8Tw2Oi6zEDb07R/L0yDOKFEFlse+IgLbfz3Lo0=", 
"user"=>{"confirmation_token"=>"", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]"}, 
"commit"=>"Confirm Account"} 

肯定有URL中的道理,但...這實際上是去什麼地方,但!

+0

爲什麼不只是:url => confirm_account_path? – Gal

回答

1

在我看來,像confirm_account_path不存在?

如果您沒有手動設置路線,則可以繼續並在config/routes.rb文件中將其設置爲confirmations#confirm_account

或者,如果您將Devise設置爲使用您的ConfirmationsController,則使用new_user_confirmation_path也可能會(也可能不會)工作。在控制檯中鍵入rake routes以查看可用路由。他們應該導致ConfirmationsControllerconfirm_account操作。

編輯:嘗試編輯您的路線文件如下。

devise_for :user, :controllers => {:confirmations => "confirmations", :registrations => "registrations" } do 
    match "/confirm_account" => "confirmations#confirm_account" 
end 

認爲斜槓是confirm_account之前重要的,因爲它是目前devise_for塊(等同於devise_scope)內。否則它可能會去users/confirm_account

編輯2:在控制器中使用params[:user][:confirmation_token]而不是params[:account][:confirmation_token]。但是目前看起來確認令牌是空白的。

+0

看我的編輯。一切似乎都是爲了對吧? –

+0

你說得對,一切似乎都對。使用PUT的原因是什麼?我會嘗試在路由文件中將「put」改爲「match」。 – joseph

+0

如果我改變它匹配,我得到'找不到路徑的設計映射「/ confirm_account」。也許你忘了把你的路線包裹在範圍塊內?例如:devise_scope:user do match「/ some/route」=>「some_devise_controller」end' –