Use both Account and User tables with Devise

后端 未结 3 1938
感动是毒
感动是毒 2020-12-02 08:52

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 (pr

3条回答
  •  情深已故
    2020-12-02 09:37

    Couldn't you use something like what's covered in these RailsCasts?:

    http://railscasts.com/episodes/196-nested-model-form-part-1

    http://railscasts.com/episodes/197-nested-model-form-part-2

    You could setup your models as described in those screencasts, using accepts_nested_attributes_for.

    Then, your views/devise/registrations/new.html.erb form would be for :user like normal, and could include a nested form for :account.

    So something like this within that default form:

    <%= f.fields_for :account do |account_form| %>
    

    <%= account_form.label :name, "Account Name", :class => "label" %> <%= account_form.text_field :name, :class => "text_field" %> (e.g., enter your account name here)

    <%= account_form.label :company, "Company Name", :class => "label" %> <%= account_form.text_field :company, :class => "text_field" %>

    <% end %>

    This is sample code from an app I'm working on and I'm using the simple_form gem, so the helpers used in your app may be different, but you'll probably get the idea.

    So when a user is created (when they register), they can also fill in the info that'll be used by the Account model to create their account once they hit the "Sign Up" button.

    And you may want to set an attribute on that user like "admin" too...sounds like this initial user will be the "admin" user for the company, though other users may have access too.

    Hope that helps.

提交回复
热议问题