在视图中访问时,将删除从属模型

时间:2017-02-03 13:43:53

标签: ruby-on-rails

我有合作伙伴模型和地址模型。合作伙伴有一个地址,地址属于合作伙伴。但是,当我访问合作伙伴视图时,地址字段中的数据将被删除。

合作伙伴模型:



 has_many :dependents, dependent: :destroy, inverse_of: :partner
  has_one :address, dependent: :destroy, autosave: true, inverse_of: :partner
  delegate :street, :number, :type_home, :neighborhood, :city, :state, to: :address
  accepts_nested_attributes_for :address, allow_destroy: true
  accepts_nested_attributes_for :dependents, allow_destroy: true




地址模型



class Address < ApplicationRecord
  belongs_to :partner, optional: true
  accepts_nested_attributes_for :partner
end
&#13;
&#13;
&#13;

日志

Address Error

修改

合作伙伴控制器

&#13;
&#13;
class PartnersController < ApplicationController
  before_action :set_partner, only: [:show, :edit, :update, :destroy]

  # GET /partners
  # GET /partners.json


  def index
    @partners = Partner.all
  end

  # GET /partners/1
  # GET /partners/1.json
  def show
  end

  # GET /partners/new
  def new
    @partner = Partner.new
    @partner.build_address
    @partner.dependents.build
  end

  def consultar
    @partners = Partner.all
  end

  def excluir
    @partners = Partner.all
  end

  # GET /partners/1/edit
  def edit
    @partner.build_address
    @partner.dependents.build
  end

  # POST /partners
  # POST /partners.json
  def create
    @partner = Partner.new(partner_params)

    respond_to do |format|
      if @partner.save
        format.html { redirect_to @partner, notice: 'O associado foi cadastrado com sucesso.' }
        format.json { render :show, status: :created, location: @partner }
      else
        format.html { render :new }
        format.json { render json: @partner.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /partners/1
  # PATCH/PUT /partners/1.json
  def update
    respond_to do |format|
      if @partner.update(partner_params)
        format.html { redirect_to @partner, notice: 'O cadastro do associado foi atualizado com sucesso.' }
        format.json { render :show, status: :ok, location: @partner }
      else
        format.html { render :edit }
        format.json { render json: @partner.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /partners/1
  # DELETE /partners/1.json
  def destroy
    @partner.destroy
    respond_to do |format|
      format.html { redirect_to partners_url, notice: 'Partner was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_partner
      @partner = Partner.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def partner_params
      params.require(:partner).permit(:name, :registry_number, :institution, :organ, :secretary, :position, :base_salary, :rg, :cpf, :address_id, :email, :phone_number, :phone_number2, :birth_city, :birthday, :spouse,  :union_admission, :start_date, :address_attributes => [:id, :street, :number, :type_home, :neighborhood, :city, :state, :zipcode, :partner_id], :dependents_attributes => [:id, :name, :partner_id, :_destroy])
    end
end
&#13;
&#13;
&#13;

_form.html.erb / _edit.html.erb

&#13;
&#13;
/*edit.html.erb*/

<h1 class="chamada">Editar Associado</h1>

<%= render 'form', partner: @partner %>


  
  
/* form.html.erb */
  <%= form_for(partner) do |f|  %>
    <% if partner.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(partner.errors.count, "erro(s)") %> não deixa(m) o cadastro ser efetuado:</h2>
          <ul>
            <% partner.errors.full_messages.each do |message| %>
                <li><%= message %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

  <h3>Dados Funcionais</h3>
  <div class="dados-funcionais">
        <div class="row">
            <div class="field col-md-4">
                <%= f.label :registry_number %>
                <%= f.number_field :registry_number %>
            </div>

            <div class="field col-md-4">
                <%= f.label :name %>
                <%= f.text_field :name %>
            </div>

            <div class="field col-md-4">
                <%= f.label :institution %>
                <%= f.text_field :institution %>
            </div>
    </div>

    <div class="row">
            <div class="field col-md-4"><br>
               <%= f.label :secretary %>
               <%= f.collection_select(:secretary, Secretary.all, :name, :name, prompt: true) %>
            </div> <br>


            <div class="field col-md-4">
              <%= f.label :position %>
              <%= f.text_field :position %>
            </div>

            <div class="field col-md-4">
              <%= f.label :base_salary %>
              <%= f.text_field :base_salary %>
            </div>

            <div class="field organ-select">
              <%= f.label :organ %>
              <%= select("partner", "organ", options_for_select([["Prefeitura Municipal de Bertioga", "PMB"],["Câmara Municipal de Bertioga", "CMB"],["Bertprev", "BTP"]]), prompt: true) %>
            </div>
    </div>
  </div>

  <h3>Dados Pessoais</h3>
  <div class="dados-pessoais">
      <div class="row">
            <div class="field col-md-4">
              <%= f.label :RG %>
              <%= f.text_field :rg, :placeholder => "__-___-___-_" %>
            </div>

            <div class="field col-md-4">
              <%= f.label :CPF %>
              <%= f.text_field :cpf, :placeholder => "___-___-___-__" %>
            </div>

            <div class="field col-md-4">
              <%= f.label :email %>
              <%= f.text_field :email %>
            </div>
      </div>

      <div class="row linha">
            <div class="field col-md-4">
              <%= f.label :phone_number %>
              <%= f.text_field :phone_number, :placeholder => "(__) _____-____" %>
            </div>

            <div class="field col-md-4">
              <%= f.label :phone_number2 %>
              <%= f.text_field :phone_number2, :placeholder => "(__) ____-____" %>
            </div>

            <div class="field col-md-4">
              <%= f.label :birth_city %>
              <%= f.text_field :birth_city %>
            </div>
      </div>
      <div class="row linha">
            <div class="field col-md-4 partner_birthday">
              <%= f.label :birthday %>
              <%= f.date_select :birthday, start_year: 1940 %>
            </div>

            <div class="field col-md-4">
              <%= f.label :spouse %>
              <%= f.text_field :spouse %>
            </div>
     </div>
  </div>

  <h3>Dados Residenciais</h3>
  <div class="dados-residenciais">
     <%= f.fields_for :address do |ff| %>
          <div class="row">
                  <div class="field col-md-4">
                    <%= ff.label :street %>
                    <%= ff.text_field :street %>
                  </div>

                  <div class="field col-md-4">
                    <%= ff.label :number %>
                    <%= ff.text_field :number %>
                  </div>

                  <div class="field col-md-4">
                    <%= ff.label :neighborhood %>
                    <%= ff.text_field :neighborhood %>
                  </div>
          </div>

          <div class="row linha">
                 <div class="field partner_type_home col-md-4">
                   <%= ff.label :type_home %>
                   <%= select("address", "type_home", options_for_select([["Apartamento", "APTO"],["Casa", "CASA"]]), prompt: true) %>
                 </div>

                <div class="field col-md-4">
                  <%= ff.label :city %>
                  <%= ff.text_field :city %>
                </div>

                <div class="field col-md-4">
                  <%= ff.label :state %>
                  <%= ff.text_field :state %>
                </div>

                <div class="field">
                  <%= ff.label :zipcode %>
                  <%= ff.text_field :zipcode %>
                </div>
          </div>
     <% end %>
   </div>


            <h3>Dependentes</h3>
            <div id="dependent-fields">
              <span class="adicionar-dependente"><%=
                link_to_add_association 'Adicionar Dependente', f, :dependents,
                                        'data-association-insertion-node' => "#dependent-fields ul"
              %></span>
                <ul>
                    <%= f.fields_for :dependents do |dependent| %>
                    <%= render partial: 'dependent_fields', locals: {f: dependent} %>
                    <% end %>
                </ul>
            </div>
      </div>

    <div class="dates">
          <div class="date1">
            <%= f.label :union_admission %> <br>
            <%= f.date_select :union_admission, start_year: 1940 %>
          </div>
          <div class="date2">
            <%= f.label :start_date %> <br>
            <%= f.date_select :start_date, start_year: 1940 %>
          </div>
    </div>

    <div class="actions">
        <%= f.submit %>
    </div>

<% end %>
  
&#13;
&#13;
&#13;

0 个答案:

没有答案