多模型嵌套表单,无法将用户添加到当前帐户 [英] Multi-model nested form, can't add users to current account

查看:86
本文介绍了多模型嵌套表单,无法将用户添加到当前帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处都在寻找解决方案,但没有提出任何解决方案.

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

有效的部分:我的应用允许客户使用嵌套表单创建帐户.收集的数据在四个模型中创建记录-帐户,用户,accounts_users(因为一个用户可以与多个帐户关联)和配置文件(用于存储用户的fname,lname,电话等).

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!

这是嵌套表格...

<%= 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>

ApplicationController将所有内容都限定为current_account,如下所示:

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

UsersController

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

模型如下:

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:事实证明,我需要在User模型中进行密码+ password_comfirmation验证,这阻止了我添加没有这些字段的其他用户.我注释掉了这些验证,并在"new"操作中删除了行:current_account.accounts_users.build(),并在"create"操作中添加了行:@ user.accounts_users.build(:account_id => current_account.id).

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.

推荐答案

"我希望用户能够使用下面的表格向其帐户中添加更多用户.我假设您的意思是个人资料(因为您的嵌套表单位于个人资料上)?

"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)?

如果是这种情况,我认为您的UsersController的create动作不是通过使用new将配置文件与用户相关联.

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

尝试一下...

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

如果您希望用户与帐户相关联,则需要将新的帐户和创建操作放入AccountsController中,并执行类似于用户和个人资料记录的嵌套关联的操作.

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!

这篇关于多模型嵌套表单,无法将用户添加到当前帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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