Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- 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
- 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
- resources :accounts, :only => [:index, :new, :create, :destroy]
- 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
- <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 %>
- <%= f.fields_for :account do |account_form| %>
- <div>
- <p>
- <%= account_form.label :name, "Account Name", :class => "label" %>
- <%= account_form.text_field :name, :class => "text_field" %>
- <span class="description">(e.g., enter your account name here)</span>
- </p>
- </div>
- <div>
- <p>
- <%= account_form.label :company, "Company Name", :class => "label" %>
- <%= account_form.text_field :company, :class => "text_field" %>
- </p>
- </div>
- <% end %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement