覆盖devise注册控制器

我在基于不同模型的registry单中添加了一个字段,请参阅如何使用devise模型的嵌套属性来获取血腥详细信息。 这部分工作正常。

现在的问题是,当我保存时,它是由devise提供的在该字段(公司)上的Activerecord::UnknownAttributeError的注册控制器的创build操作失败。

我假设我需要重写注册控制器,或者有更好/更简单的方法,我应该接近这个?

在你的表单中是否传递了任何其他属性,通过不属于你的用户模型的质量分配,还是任何嵌套模型?

如果是这样,我相信在这种情况下触发ActiveRecord :: UnknownAttributeError。

否则,我想你可以创build自己的控制器,通过生成这样的东西:

 # app/controllers/registrations_controller.rb class RegistrationsController < Devise::RegistrationsController def new super end def create # add custom create logic here end def update super end end 

然后告诉devise使用该控制器,而不是默认的:

 # app/config/routes.rb devise_for :users, :controllers => {:registrations => "registrations"} 

使用命名空间覆盖Devise控制器和视图的更好和更有组织的方式:

创build以下文件夹:

 app/controllers/my_devise app/views/my_devise 

把你想要覆盖的所有控制器放到app / controllers / my_devise中,并将MyDevise命名空间添加到控制器类名中。 Registrations示例:

 # app/controllers/my_devise/registrations_controller.rb class MyDevise::RegistrationsController < Devise::RegistrationsController ... def create # add custom create logic here end ... end 

相应地改变你的路线:

 devise_for :users, :controllers => { :registrations => 'my_devise/registrations', # ... } 

将所有必需的视图从Devise gem文件夹复制到app/views/my_devise ,或使用rails generate devise:views app/views/my_devise rails generate devise:views ,删除你不重写的视图,并将my_devise文件夹重命名为my_devise

这样你就可以把所有东西整齐地组织在两个文件夹中。

我相信有一个比重写RegistrationsController更好的解决scheme。 我做了完全一样的事情(我只有组织而不是公司)。

如果你在模型和视图层面正确地设置了你的嵌套表单,那么所有东西都像魅力一样。

我的用户模型:

 class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, :lockable and :timeoutable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :owned_organizations, :class_name => 'Organization', :foreign_key => :owner_id has_many :organization_memberships has_many :organizations, :through => :organization_memberships # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :username, :owned_organizations_attributes accepts_nested_attributes_for :owned_organizations ... end 

我的组织模式:

 class Organization < ActiveRecord::Base belongs_to :owner, :class_name => 'User' has_many :organization_memberships has_many :users, :through => :organization_memberships has_many :contracts attr_accessor :plan_name after_create :set_owner_membership, :set_contract ... end 

我的看法:'devise / registrations / new.html.erb'

 <h2>Sign up</h2> <% resource.owned_organizations.build if resource.owned_organizations.empty? %> <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> <p><%= f.label :name %><br /> <%= f.text_field :name %></p> <p><%= f.label :email %><br /> <%= f.text_field :email %></p> <p><%= f.label :username %><br /> <%= f.text_field :username %></p> <p><%= f.label :password %><br /> <%= f.password_field :password %></p> <p><%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation %></p> <%= f.fields_for :owned_organizations do |organization_form| %> <p><%= organization_form.label :name %><br /> <%= organization_form.text_field :name %></p> <p><%= organization_form.label :subdomain %><br /> <%= organization_form.text_field :subdomain %></p> <%= organization_form.hidden_field :plan_name, :value => params[:plan] %> <% end %> <p><%= f.submit "Sign up" %></p> <% end %> <%= render :partial => "devise/shared/links" %> 

您可以为devise定制生成视图和控制器。

使用

 rails g devise:controllers users -c=registrations 

 rails g devise:views 

它会把特定的控制器和视图从gem复制到你的应用程序中。

接下来,告诉路由器使用这个控制器:

 devise_for :users, :controllers => {:registrations => "users/registrations"} 

非常简单的方法只要去terminal和types如下

 rails g devise:controllers users //This will create devise controllers in controllers/users folder 

接下来使用自定义视图

 rails g devise:views users //This will create devise views in views/users folder 

现在在你的route.rb文件中

 devise_for :users, controllers: { :sessions => "users/sessions", :registrations => "users/registrations" } 

您也可以添加其他控制器。 这将使devise使用用户文件夹中的控制器和用户文件夹中的视图。

现在,您可以根据需要自定义视图,并将您的逻辑添加到控制器/用户文件夹中的控制器。 请享用 !

您还可以在您的控制器path中创build文件夹,并将整个devise控制器复制/粘贴到其中。 这不需要额外的configuration。

所以对于注册控制器,它将是app/controllers/devise/registrations_controller.rb ,然后将deviseregistrations_controller.rb源文件复制到其中。 你可以从github https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb得到它,或者你可以在你的gem来源追踪它,或者如果你使用RubyMine在类行;class Devise::RegistrationsController一个像地球符号一样的地球符号…点击它,它会为你打开Devise Registration Controller文件。 然后复制/粘贴。

创build控制器注册并通过预定义的Devise :: RegistrationsController类覆盖其inheritance的类

 # app/controllers/registrations_controller.rb class RegistrationsController < Devise::RegistrationsController def new super end def create # add custom create logic here end def update super end end 

经过这一组路线:

 # app/config/routes.rb devise_for :users, :controllers => {:registrations => "registrations"}