使用与设计帐户和用户表 [英] Use both Account and User tables with Devise

查看:113
本文介绍了使用与设计帐户和用户表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rails 3.1.0的工作,并制定1.4.8,并且是新来的两个。

I'm working with Rails 3.1.0 and Devise 1.4.8, and am new to both.

我想允许多个用户帐户。注册的第一个用户创建帐户(可能为他们的公司),则该用户可以添加更多的用户。用户总是与只有一个帐户。

I want to allow multiple users for an account. The first user to sign up creates the account (probably for their company), then that user can add more users. A user is always linked to exactly one account.

我有用户和帐户表。简称车型:

I have Users and Accounts tables. Abbreviated models:

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

现在的问题是,当第一个用户注册,我怎么同时创建帐户和用户?

The question is, when the first user signs up, how do I create both the Account and User?


  • 我是否需要修改/覆盖设计registrations_controller,
    类似的回答这里?我无法弄清楚如何
    创建帐号,然后把它传递给设计用于创建用户。

  • Do I need to modify/override the Devise registrations_controller, something like the answer here? I couldn't figure out how to create the Account then pass it to Devise for creating the User.

ACCOUNT_ID已经在用户模式。我要补充帐户名
和ACCOUNT_TYPE到用户模式,并创建一个新帐户
记录,如果ACCOUNT_ID不通过呢?在这种情况下,我试图隐藏从设计创建帐户,但不知道会工作,因为我还需要提示输入帐户名和ACCOUNT_TYPE注册页面。

account_id is already in the User model. Should I add account_name and account_type to the User model, and create a new the Account record if account_id is not passed in? In this case, I'm trying to hide Account creation from Devise, but not sure that will work since I still need to prompt for account_name and account_type on the registration page.

推荐答案

最后得到这个使用嵌套属性的工作。正如在肯顿的答复意见讨论的,例如是相反的。如果你想每个帐户多个用户,你必须首先创建的帐户,然后用户 - 即使你只能创建一个用户开始。然后,你写你自己的账户控制器和视图,绕过设计视图。发送确认电子邮件等功能设计似乎仍然工作,如果你只需要直接创建一个用户,即该功能必须在制定的的自动魔法的东西零件模型的;它不需要使用设计控制器。

Finally got this to work using nested attributes. As discussed in the comments on Kenton's answer, that example is reversed. If you want multiple users per account, you have to create the Account first, then the User--even if you only create one user to start with. Then you write your own Accounts controller and view, bypassing the Devise view. The Devise functionality for sending confirmation emails etc. still seems to work if you just create a user directly, i.e. that functionality must be part of automagical stuff in the Devise model; it doesn't require using the Devise controller.

摘录的有关文件:

模式在app /型号

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的模型试验

spec/models/account_spec.rb RSpec model test

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

的config / routes.rb中

  resources :accounts, :only => [:index, :new, :create, :destroy]

控制器/ 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是相当​​挑剔复数与单数。既然我们说的帐号的has_many用户:

Rails is quite picky about plural vs. singular. Since we say Account has_many Users:


  • 预计在模型users_attributes(不user_attributes)和试验

  • 它期望一个的的阵列的哈希用于测试,即使在阵列中只有一个元素,因此[]围绕{用户属性}

  • 预计在控制器@ account.users.build。我是不是能够得到语法f.object.build_users直接在视图中工作。

  • it expects users_attributes (not user_attributes) in the model and tests
  • it expects an array of hashes for the test, even if there is only one element in the array, hence the [] around the {user attributes}.
  • it expects @account.users.build in the controller. I was not able to get the f.object.build_users syntax to work directly in the view.

这篇关于使用与设计帐户和用户表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆