2013-07-09 120 views
0

我正在使用Devise進行身份驗證/註冊。我有一個編輯用戶頁面,該頁面設計爲我生成的,看起來像這樣:設計定製更新控制器

<h2>Edit <%= resource_name.to_s.humanize %></h2> 

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %> 
    <%= devise_error_messages! %> 

    <div><%= f.label :email %><br /> 
    <%= f.email_field :email, :autofocus => true %></div> 

    <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> 
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div> 
    <% end %> 

    <div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> 
    <%= f.password_field :password, :autocomplete => "off" %></div> 

    <div><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %></div> 

    <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> 
    <%= f.password_field :current_password %></div> 

    <div><%= f.submit "Update" %></div> 
<% end %> 

<h3>Cancel my account</h3> 

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %></p> 

<%= link_to "Back", :back %> 

的問題是,我的「用戶」比色器件爲我產生了更多的領域,我想通過那些以及處理由前端用戶操作創建的各種場景。基本上,當我點擊「更新」提交表單時,我需要定製Devise在後臺執行的任何操作。

我該如何重寫Devise控制器來實現我所需要的,還是讓提交按鈕路由到具有我需要的操作的不同更新控制器?

這裏是我的路線(經紀人和租房都是一個類型的用戶,同義詞這些目的):

devise_for:中間商 devise_for:租房

回答

3

可以通過聲明您的自定義重寫設計控制器註冊控制器的Devise::RegistrationsController一個子類:

# app/controllers/authentication/registrations_controller.rb 
class Authentication::RegistrationsController < Devise::RegistrationsController 

    def new 
    # custom logic 
    super 
    end 

end 

然後,定義在routes.rb路線自定義控制器:

devise_for :brokers, :controllers => { :registrations => "authentication/registrations" } 
devise_for :renters, :controllers => { :registrations => "authentication/registrations" } 
+0

我正在嘗試這個爲我的更新,但它似乎並沒有指向正確的控制器。這是否適用於PUT請求? – user1427661

+0

我有一個小錯字,嘗試更新我的路線。是的,這應該適用於所有RESTful路由,包括使用PUT方法(即更新)的路由。 – zeantsoi