ruby-on-rails - 将 Account 和 User 表与 Devise 一起使用

标签 ruby-on-rails ruby-on-rails-3 authentication devise

我正在使用 Rails 3.1.0 和 Devise 1.4.8,并且对两者都是新手。

我想允许多个用户使用一个帐户。第一个注册用户创建帐户(可能是为他们的公司),然后该用户可以添加更多用户。用户始终与一个帐户相关联。

我有用户和帐户表。缩略型号:

class User < ActiveRecord::Base
  belongs_to :account
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :confirmable, :lockable, :timeoutable
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

class Account < ActiveRecord::Base
  has_many :users, :dependent => :destroy
  attr_accessible :name, :account_type
end

问题是,当第一个用户注册时,我如何创建帐户和用户?
  • 我是否需要修改/覆盖设计注册 Controller ,
    类似于答案 here ?我不知道怎么做
    创建帐户,然后将其传递给设计以创建用户。
  • account_id 已经在 User 模型中。我应该添加 account_name
    和 account_type 到 User 模型,并创建一个新的 Account
    记录account_id是否传入?在这种情况下,我试图从 Devise 隐藏帐户创建,但不确定是否会起作用,因为我仍然需要在注册页面上提示 account_name 和 account_type。
  • 最佳答案

    最后使用嵌套属性让它工作。正如对肯顿答案的评论中所讨论的那样,这个例子是相反的。如果您希望每个帐户有多个用户,则必须先创建帐户,然后再创建用户——即使您一开始只创建一个用户。然后你编写自己的 Accounts Controller 和 View ,绕过 Devise View 。如果您直接创建用户,用于发送确认电子邮件等的设计功能似乎仍然有效,即该功能必须是设计模型中自动魔法的一部分;它不需要使用设计 Controller 。

    相关文件的摘录:

    型号 在应用程序/模型中

    class Account < ActiveRecord::Base
      has_many :users, :inverse_of => :account, :dependent => :destroy
      accepts_nested_attributes_for :users
      attr_accessible :name, :users_attributes
    end
    
    class User < ActiveRecord::Base
      belongs_to :account, :inverse_of => :users
      validates :account, :presence => true
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable,
             :confirmable, :lockable, :timeoutable
      attr_accessible :email, :password, :password_confirmation, :remember_me
    end
    

    规范/模型/account_spec.rb RSpec 模型测试
    it "should create account AND user through accepts_nested_attributes_for" do
      @AccountWithUser = { :name => "Test Account with User", 
                           :users_attributes => [ { :email => "user@example.com", 
                                                    :password => "testpass", 
                                                    :password_confirmation => "testpass" } ] }
      au = Account.create!(@AccountWithUser)
      au.id.should_not be_nil
      au.users[0].id.should_not be_nil
      au.users[0].account.should == au
      au.users[0].account_id.should == au.id
    end
    

    配置/路由.rb
      resources :accounts, :only => [:index, :new, :create, :destroy]
    

    Controller /accounts_controller.rb
    class AccountsController < ApplicationController
    
      def new
        @account = Account.new
        @account.users.build # build a blank user or the child form won't display
      end
    
      def create
        @account = Account.new(params[:account])
        if @account.save
          flash[:success] = "Account created"
          redirect_to accounts_path
        else
          render 'new'
        end
      end
    
    end
    

    意见/帐户/new.html.erb 看法
    <h2>Create Account</h2>
    
    <%= form_for(@account) do |f| %>
      <%= render 'shared/error_messages', :object => f.object %>
      <div class="field">
        <%= f.label :name %><br />
        <%= f.text_field :name %>
      </div>
    
      <%= f.fields_for :users do |user_form| %>
        <div class="field"><%= user_form.label :email %><br />
        <%= user_form.email_field :email %></div>
        <div class="field"><%= user_form.label :password %><br />
        <%= user_form.password_field :password %></div>
        <div class="field"><%= user_form.label :password_confirmation %><br />
        <%= user_form.password_field :password_confirmation %></div>
      <% end %>
    
      <div class="actions">
        <%= f.submit "Create account" %>
      </div>
    <% end %>
    

    Rails 对复数和单数非常挑剔。既然我们说 Account has_many Users:
  • 它需要模型中的 users_attributes(不是 user_attributes)并测试
  • 它需要一个用于测试的哈希数组,即使数组中只有一个元素,因此 {user attributes} 周围有 []。
  • 它期望 Controller 中的@account.users.build。我无法让 f.object.build_users 语法直接在 View 中工作。
  • 关于ruby-on-rails - 将 Account 和 User 表与 Devise 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8305120/

    相关文章:

    ruby-on-rails - HABTM 查询,返回没有关联的记录

    ruby-on-rails-3 - Rails Routing : One controller. 有类型的一个模型。多条路线

    c# - 无法从 C# 使用 AAD 连接到 Azure SQL 数据库

    jquery - 源 map 请求导致 404 错误

    ruby-on-rails - 在 Ruby on Rails 中,我如何告诉模型它应该有一个涵盖两个不同领域的唯一约束?

    javascript - rails : Heroku assets in vendor getting 404

    ruby-on-rails-3 - 在 Tomcat 中运行的 Jruby on Rails 应用程序中,使用 java servlet 存储不起作用

    ruby-on-rails - 在 ruby​​ on rails 中删除数组的替代元素

    node.js - Django 和 NodeJs - 管理身份验证

    authentication - 什么是私钥挑战?