巢状表格:如果未输入儿童的资讯,则不应储存 [英] Nested form: shouldn't save if no information for child is entered

查看:59
本文介绍了巢状表格:如果未输入儿童的资讯,则不应储存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套表单来注册一个新组织和1个成员(组织及其成员之间的比例为1:许多).两者都在模型文件和迁移文件中进行了验证.

I have a nested form to sign up a new organization and 1 member (1:many relationship between organization and its members). Both have validations in the model file and the migration file.

问题:如果不正确输入两者的信息,我希望表格不签署任何内容(不是组织或成员).

The issue: I would like the form not to sign up anything (not organization nor member) if not information for both are entered correctly.

当前,当我注册一个新组织但将该成员的所有变量保留为空时,它将在没有错误消息的情况下对组织进行注册,并且不对任何成员进行注册.

Currently, when I sign up a new organization but leave all the variables for the member empty it signs up the organization without an error message and it signs up no member.

相反,我希望它也尝试写该成员,然后产生错误消息,因为该成员的某些变量不允许为空.由于该错误,它也不应保存组织,直到成员信息正确为止.

Instead I would want it to try to write the member as well, and then produce an error message because certain variables for the member are not allowed to be empty. Because of that error it should not save the organization either untill also the member information is correct.

如果我输入一些有关会员的信息,这确实可以正常工作:如果该会员那时无效,则不会保存组织或会员并给出错误.如果我输入了有效的会员信息,而没有输入组织的信息,它也可以正常工作:它会产生错误,并且不保存任何内容.但是,如果我输入组织的有效信息而不输入成员的信息,则它不起作用:它不会产生任何错误并仅保存组织.

This does work correctly if I enter some information for member: if the member then is invalid it doesn't save organization nor member and gives the errors. It also works correctly if I enter valid info for member and no info for organization: it produces error and saves nothing. But it does not work if I enter valid info for organization and no info for member: it produces no errors and saves only the organization.

我应该如何更改代码以进行调整?

How should I change my code to adjust this?

视图:

<%= render partial: "registrationform", locals: { url: organizations_path } %>

部分/形式:

  <%= form_for @organization, url: url do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <h4>Details of the organization:</h4>
    <%= f.text_field :name, class: 'form-control' %>

    <%= f.fields_for :members do |p| %>
      <h4>Your personal details:</h4>
      <%= p.text_field :username, class: 'form-control' %>
      <%= p.email_field :email, class: 'form-control' %>
      <%= p.password_field :password, class: 'form-control' %>
      <%= p.password_field :password_confirmation, class: 'form-control' %>
    <% end %>

    <%= f.submit "Sign up", class: "formbutton btn btn-default" %>
  <% end %>

和控制器:

  def new
    if (logged_in?)
      flash[:danger] = "You're already logged in"
      redirect_to root_url
    end
    @organization = Organization.new
    @member = @organization.members.build
  end

  def create
    @organization = Organization.new(new_params)
    if @organization.save
      @organization.members.each do |single_member|
        single_member.send_activation_email
      end
      flash[:success] = "Please check your email to activate your account."
      redirect_to root_url
    else                            
      @organization.members.build if @organization.members.blank?
      render 'new'
    end
  end

private
def new_params
  params.require(:organization).permit(:name,
                 members_attributes: [:email,
                                      :username,
                                      :password,
                                      :password_confirmation
                                      ])
end

推荐答案

在您的组织模型中,您可以检查成员

In your organization model, you can check for member

class Organization
   validate :check_member
   def check_member
      if members.empty?
         errors.add(:base, 'Member is not present')
      end
   end
end

这篇关于巢状表格:如果未输入儿童的资讯,则不应储存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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