Multi-model nested form, can't add users to current account

北战南征 提交于 2019-12-25 01:35:31

问题


I've searched everywhere for a solution but haven't come up with any.

The part that works: My app allows customers to create an account using a nested form. The data collected creates records in four models - accounts, users, accounts_users (because a user can be associated with many accounts), and profile (to store the user's fname, lname, phone, etc).

That part that doesn't work: Once logged in, I want the users to be able to add more users to their account using the form below. I don't receive any errors upon submit but I am brought back to the same form with no additional records created. Any help would be awesome!

Here is the nested form...

<%= form_for @user, :validate => true do |f| %>
<fieldset>
    <%= f.fields_for :profile do |p| %>
    <div class="field">
      <%= p.label :first_name %>
      <%= p.text_field :first_name %>
    </div>
    <div class="field">
      <%= p.label :last_name %>
      <%= p.text_field :last_name %>
    </div>
    <div class="field">
      <%= p.label :phone %>
      <%= p.text_field :phone %>
    </div>
    <% end %>
    <div class="field">
        <%= f.label :email %>
        <%= f.text_field :email %>
    </div>
    <div class="actions">
        <%= f.submit 'Create New User', :class => "btn btn-large btn-success" %>
        <%= cancel %>   
    </div>
</fieldset>

The ApplicationController scopes everything to the current_account like so:

def current_account
  @current_account ||= Account.find_by_subdomain(request.subdomain) if request.subdomain
end

The UsersController

def new
  @user = User.new  
  @user.build_profile()
  #current_account.accounts_users.build()  #Edit2: This line was removed

  respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @user }
end

def create
  @user = User.new(params[:user])
  @user.accounts_users.build(:account_id => current_account.id) #Edit2: This line was added
  if @user.save
    # Send Email and show 'success' message
    flash[:success] = 'An email has been sent to the user'
  else
    # Render form again
    render 'new'
  end
end

Models look like this:

class Account < ActiveRecord::Base
  attr_accessible :name, :subdomain, :users_attributes
  has_many :accounts_users
  has_many :users, :through => :accounts_users
  accepts_nested_attributes_for :users
end

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation, :profile_attributes
  has_many :accounts_users
  has_many :accounts, :through => :accounts_users
  has_one :profile
  accepts_nested_attributes_for :profile
end

class AccountsUser < ActiveRecord::Base
  belongs_to :account
  belongs_to :user
end

class Profile < ActiveRecord::Base
  belongs_to :user
  attr_accessible :first_name, :last_name, :phone
end

Edit2: It turns out that I had required a password + password_comfirmation validation in the User model which prevented me from adding another user without these fields. I commented out these validations plus removed the line: current_account.accounts_users.build() in the 'new' action and added the line: @user.accounts_users.build(:account_id => current_account.id) in the 'create' action.


回答1:


"I want the users to be able to add more users to their account using the form below." I assume you mean profiles (since your nested form is on profiles)?

If that's the case, I think your UsersController's create action isn't associating the profiles with users by using new.

Try this...

def new
  @user = User.build
  @profile = @user.profiles.build #build adds the profile to user's associated collection of profiles, but new doesn't

  ...
end

def create
  @user = User.build(params[:user])
  if @user.save
    ....
  end
end

If you want the user to be associated with account, then you need to put the new and create actions in the AccountsController and do something similar to nest association of the users and profiles records.

Btw, the reason that it went back to new is because you render new at the end of the create, in case that's also part of the question. Hope that helps!



来源:https://stackoverflow.com/questions/11266125/multi-model-nested-form-cant-add-users-to-current-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!