设计自定义更新控制器

时间:2013-07-09 22:53:11

标签: ruby-on-rails ruby devise

我正在使用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:租房者

1 个答案:

答案 0 :(得分:3)

您可以通过将自定义注册控制器声明为Devise::RegistrationsController的子类来覆盖Devise控制器:

# 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" }