首页 文章

覆盖设计注册控制器

提问于
浏览
220

我在基于不同模型的注册表单中添加了一个字段,请参阅How do I use nested attributes with the devise model了解详细信息 . 这部分工作正常 .

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

我假设我需要覆盖注册控制器,还是有更好/更简单的方法我应该接近这个?

6 回答

  • 10

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

    如果是这样,我相信在这个实例中触发了ActiveRecord :: UnknownAttributeError .

    否则,我认为你可以通过生成这样的东西来创建自己的控制器:

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

    使用命名空间覆盖Devise控制器和视图的更好,更有条理的方法:

    创建以下文件夹:

    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 ,删除未覆盖的视图,并将 devise 文件夹重命名为 my_devise .

    通过这种方式,您可以将所有内容整齐地组织在两个文件夹中 .

  • 10

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

    如果您正确设置嵌套表单,在模型和视图级别,一切都像魅力一样 .

    我的用户型号:

    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
    

    我的观点:'设计/注册/ 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 %>
    <%= f.text_field :name %></p> <p><%= f.label :email %>
    <%= f.text_field :email %></p> <p><%= f.label :username %>
    <%= f.text_field :username %></p> <p><%= f.label :password %>
    <%= f.password_field :password %></p> <p><%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation %></p> <%= f.fields_for :owned_organizations do |organization_form| %> <p><%= organization_form.label :name %>
    <%= organization_form.text_field :name %></p> <p><%= organization_form.label :subdomain %>
    <%= 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" %>
  • 339

    您可以生成视图和控制器以进行设计自定义 .

    使用

    rails g devise:controllers users -c=registrations
    

    rails g devise:views
    

    它会将gem中的特定控制器和视图复制到您的应用程序中 .

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

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

    非常简单的方法只需转到终端和类型如下

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

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

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

  • 33

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

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

相关问题