简体   繁体   中英

Rails 3 and Devise: How to register an account and user in the registration page?

I need a registration page to record a new account that has user as a nested attribute. For example:

MODELS

class User < ActiveRecord::Base

  belongs_to :account
  validates :account_id, :presence => true
  ...

class Account < ActiveRecord::Base
  has_many :users, :dependent => :destroy
  accepts_nested_attributes_for :users
  ...

TABLES

accounts
  id: 5

users
  id: 32
  email: someuser@gmail.com
  account_id: 5

I know to override the registrations controller I have to do something like this...

class RegistrationsController < Devise::RegistrationsController
  def new
    super
  end

  def create
    super
  end
end

I have the Devise default registration page. But how can I register an account and a user that belongs to that account?

As it seems that your Devise resource is user, maybe you want to use nested attributes on users, instead of account.

Then, you will be able to do the registration using nested attributes and then assign an account with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM