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_for :brokers devise_for :renters

4

1 回答 1

4

您可以通过将自定义注册控制器声明为以下子类来覆盖设计控制器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" }
于 2013-07-09T23:22:20.013 回答