Nested form: shouldn't save if no information for child is entered

我的梦境 提交于 2019-12-24 02:22:48

问题


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?


The view:

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

The partial/form:

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

And the controller:

  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

回答1:


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


来源:https://stackoverflow.com/questions/30190890/nested-form-shouldnt-save-if-no-information-for-child-is-entered

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