如何使用茧形宝石使用具有不同关系的嵌套形式?

时间:2019-05-06 20:42:29

标签: ruby-on-rails ruby ruby-on-rails-5 cocoon-gem

我的困境已经解决了一个星期,我正在设法解决它,但是到目前为止我还无法解决,所以我正在寻求帮助。

我有一个Model Dealership和其他嵌套的(DealershipsSetting,地址,机器,操作员,卡),其中has_one关系仅适用于DealershipsSetting,因此其他关系为has_many。 当我为嵌套表单的每个模型调用build方法时,我只能在数据库表中写一些信息,例如:

  def new
    @dealership = Dealership.new
    @ dealership.build_dealerships_setting if @ dealership.dealerships_setting.blank?
    @ dealership.addresses.build if @ dealership.addresses.blank?
...
  end

  def edit
    @ dealership.build_dealerships_setting if @ dealership.dealerships_setting.blank?
    @ dealership.addresses.build if @ dealership.addresses.blank?
...
  end

我意识到:

a)我在日志中注意到的第一件事是表单被多次重定向。这是有道理的,因为我强迫他们通过build调用进行构建(至少这是我所了解的)。但这不是使用gem时的默认行为,我在另一个应用程序中进行了测试,只是通过遵循文档https://github.com/nathanvda/cocoon

来了解如何使用gem。

b)由于调用了build方法,因此表单打开了(准备接收数据),我不知道这是否是默认行为。

c)当我调用“新建”或“编辑”操作时,我不能记录多个记录(类型,2个地址,3个卡或N个运算符)

d)在文档中,我没有发现对build方法的任何调用,但在搜索中发现它试图解决我的问题,例如https://share.atelie.software/rails-nested-attributes-com-has-many-42ecf6179871

e)如果我从文档中删除调用,则按照文档中提供的示例进行操作,直到您通过link_to_add_association进行调用之前,这些字段才会显示为隐藏状态,但是不会保存嵌套表单中的任何数据。

当我这样设置我的Dealerships_controller时,我无法在数据库中添加任何寄存器:

def new    
   @dealership = Dealership.new        
end
...
def create
   @dealership = Dealership.new(dealership_params)

   respond_to do |format|
     if @dealership.save
        format.html { redirect_to users_backend_dealerships_path, notice: 'Dealership was successfully created.' }
        format.json { render :show, status: :created, location: @dealership }
     else
        format.html { render :new }
        format.json { render json: @dealership.errors, status: :unprocessable_entity }
     end
   end
 end
....

def dealership_params
  params.require(:dealership).permit(
:fantasy_name, :social_name, :cpf, :cnpj, :municipal_registration, :state_registration, :credit, :phone, :manager_email, :is_available, :credits_package_id, dealerships_setting_attributes: [:id, :credit_alert, :contract_validity, :franchise_for_rent, :due_date, :credit_value, :is_available, :_destroy],addresses_attributes: [:id, :place, :neighborhood, :cep, :state, :city, :is_available, :_destroy], machines_attributes: [:id, :name, :model, :serial_number, :mac_address, :calibration_counter, :is_available, :_destroy], operators_attributes: [:id, :name, :cpf, :card, :is_available, :_destroy ], cards_attributes: [:id, :serial, :category, :credit_package, :client, :machine, :operator, :is_available, :_destroy ])
end

像这样,我可以在数据库中按模型添加一个寄存器。

dealerships_controller

class UsersBackend :: DealershipsController <UsersBackendController
  before_action: set_dealership, only: [: show,: edit,: update,: destroy]
  before_action: get_credit_packages, only: [: edit,: update,: new]
 
 def index
    @dealerships = Dealership.includes(:dealerships_setting, :addresses, :machines, :operators, :cards)
  end

  def show
  end

  def add_credits
  end

  def new
    @dealership = Dealership.new        
    @dealership.build_dealerships_setting if @dealership.dealerships_setting.blank?
    @dealership.addresses.build if @dealership.addresses.blank?
    @dealership.machines.build if @dealership.machines.blank?
    @dealership.operators.build if @dealership.operators.blank?
    @dealership.cards.build if @dealership.cards.blank?
  end

  def edit
    @dealership.build_dealerships_setting if @dealership.dealerships_setting.blank?
    @dealership.addresses.build if @dealership.addresses.blank?
    @dealership.machines.build if @dealership.machines.blank?
    @dealership.operators.build if @dealership.operators.blank?
    @dealership.cards.build if @dealership.cards.blank?
  end

  def create
    @dealership = Dealership.new(dealership_params)

    respond_to do |format|
      if @dealership.save
        format.html { redirect_to users_backend_dealerships_path, notice: 'Dealership was successfully created.' }
        format.json { render :show, status: :created, location: @dealership }
      else
        format.html { render :new }
        format.json { render json: @dealership.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @dealership.update(dealership_params)
        format.html { redirect_to users_backend_dealerships_path, notice: 'Dealership was successfully updated.' }
        format.json { render :show, status: :ok, location: @dealership }
      else
        format.html { render :edit }
        format.json { render json: @dealership.errors, status: :unprocessable_entity }
      end
    end
  end

def destroy
    @dealership.destroy
    respond_to do |format|
      format.html { redirect_to users_backend_dealerships_path, notice: 'Dealership was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def set_dealership
      @dealership = Dealership.find(params[:id])
    end

    def dealership_params
      params.require(:dealership).permit(:fantasy_name, :social_name, :cpf, :cnpj, :municipal_registration, :state_registration, :credit, :phone, :manager_email, :is_available, :credits_package_id,
        dealerships_setting_attributes: [:id, :credit_alert, :contract_validity, :franchise_for_rent, :due_date, :credit_value, :is_available, :_destroy],
        addresses_attributes: [:id, :place, :neighborhood, :cep, :state, :city, :is_available, :_destroy],
        machines_attributes: [:id, :name, :model, :serial_number, :mac_address, :calibration_counter, :is_available, :_destroy], 
        operators_attributes: [:id, :name, :cpf, :card, :is_available, :_destroy ],
        cards_attributes: [:id, :serial, :category, :credit_package, :client, :machine, :operator, :is_available, :_destroy ]
        )
    end

    def get_credit_packages
      @credit_packages = CreditsPackage.where(media_owner: 0)
    end
end

model Dealship.rb

class Dealership < ApplicationRecord

  has_many :credits_packages

  has_one :dealerships_setting, dependent: :destroy, inverse_of: :dealership

  has_many :addresses, dependent: :destroy, inverse_of: :dealership
  has_many :machines, dependent: :destroy, inverse_of: :dealership
  has_many :operators, dependent: :destroy, inverse_of: :dealership
  has_many :cards, dependent: :destroy, inverse_of: :dealership


  accepts_nested_attributes_for :dealerships_setting, reject_if: :all_blank, allow_destroy: true

  accepts_nested_attributes_for :addresses, reject_if: :all_blank, allow_destroy: true
  accepts_nested_attributes_for :machines, reject_if: :all_blank, allow_destroy: true
  accepts_nested_attributes_for :operators, reject_if: :all_blank, allow_destroy: true
  accepts_nested_attributes_for :cards, reject_if: :all_blank, allow_destroy: true

  accepts_nested_attributes_for :users, reject_if: :all_blank, allow_destroy: true
end

模型经销商设置

class DealershipsSetting < ApplicationRecord
  belongs_to :dealership, inverse_of: :dealerships_setting
end

模型address.rb

class Address < ApplicationRecord
  belongs_to :dealership
end

form.html.erb(交易)

<%= form_with(model: [ :users_backend, @dealership], local: true) do |form| %>
     <div class="form-group">
          <strong><%= form.label :fantasy_name %></strong>
               <%= form.text_field :fantasy_name, autofocus: true, class:"text-uppercase form-control", placeholder:t('place_holders.fantasy_name') %>
     </div>
...

<%= form.fields_for :dealerships_setting, @dealership.dealerships_setting do |dealerships_setting| %>
     <%= render partial: 'dealerships_setting_fields', locals: { f: dealerships_setting } %>
  <% end %>

<div id="addresses"> 
    <%= form.fields_for :addresses do |address| %> 
<%= render partial: 'address_fields', locals: { f: address } %>
    <% end %>
    <%= link_to_add_association('Add address', form, :addresses) %>
 </div> 
....others nested forms...

_dealerships_setting_fields.html.erb

<div class="nested-fields">
  <div class="form-group">
    <strong><%= f.label :contract_validity %></strong>
    <%= f.text_field :contract_validity, class:"form-control ", placeholder:t('place_holders.contract_validity') %>
  </div> ...another fields...

_address_fields.html.erb

<div class='nested-fields'>
  <div class='form-group'>
    <strong><%= f.label :place %></strong>
    <%= f.text_field :place, class:"text-uppercase form-control", placeholder:t('place_holders.place') %>
  </div> ...another fields...

我需要什么? A-管理代理商,代理商必须具有一种配置以及一个或多个地址,机器,操作员和卡。

我将非常感谢能帮助我的任何人。

1 个答案:

答案 0 :(得分:0)

好,所以我遇到了两个主要错误:

  • 您的表单标签位于一个div中,但是您的表单内容跨越了多个div。看起来正确,但在所有情况下都不会发布正确的信息
  • 第二:对于我来说,您确实有验证阻止保存,然后发生了两件事:
    • 您没有在@credit_packages动作中设置create,因此在重新呈现表单时出现错误
    • 您没有显示任何验证错误(因此没有关于保存失败原因的反馈)

所以我做了两件事。在您的dealerships_controller中,我编辑了以下行

before_action :get_credit_packages, only: [:edit, :update, :new, :create] 

(还在创建时设置信用额度)

然后您的_form.html.erb我将form_with直接移到了row div的下面,并添加了一些基本的验证错误显示。 row div从未关闭。完整表单:

<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800"><%= action_message %></h1>
<div class="row">
  <%= form_with(model: [ :users_backend, @dealership], local: true) do |form| %>

    <p>
      <%= @dealership.errors.messages.inspect %>
    </p>


    <div class="col-lg-6"> <!-- card Dealerships Information-->
      <div class="card border-left-danger shadow mb-4">
        <a href="#collapseCardDealershipInformation" class="d-block card-header py-3" data-toggle="collapse" role="button" aria-expanded="true" aria-controls="collapseCardDealershipInformation"><h6 class="m-0 font-weight-bold text-primary"><%= t('labels.dealership_information') %></h6></a>
        <div class="collapse show" id="collapseCardDealershipInformation"> <!-- card Content - Collapse -->
          <div class="card-body">
            <div class="form-group">
              <strong><%= form.label :fantasy_name %></strong>
              <%= form.text_field :fantasy_name, autofocus: true, class:"text-uppercase form-control", placeholder:t('place_holders.fantasy_name') %>
            </div>

            <div class="form-group">
              <strong><%= form.label :social_name %></strong>
              <%= form.text_field :social_name, class:"text-uppercase form-control", placeholder:t('place_holders.social_name') %>
            </div>

            <div class="form-group row">
              <div class="col-md-6">
                <strong><%= form.label :phone %></strong>
                <%= form.text_field :phone, class:"form-control", placeholder:t('place_holders.phone') %>
              </div>
              <div class="col-md-6">
                <strong><%= form.label :manager_email %></strong>
                <%= form.text_field :manager_email, class:"text-downcase form-control", placeholder:t('place_holders.manager_email') %>
              </div>
            </div>

            <div class="form-group row">
              <div class="col-md-6">
                <strong><%= form.label :cpf %></strong>
                <%= form.text_field :cpf, class:"form-control", placeholder:t('place_holders.cpf') %>
              </div>
              <div class="col-md-6">
                <strong><%= form.label :cnpj %></strong>
                <%= form.text_field :cnpj, class:"form-control", placeholder:t('place_holders.cnpj') %>
              </div>
            </div>

            <div class="form-group row">
              <div class="col-md-6">
                <strong><%= form.label :municipal_registration %></strong>
                <%= form.text_field :municipal_registration, class:"form-control form-control-user", placeholder:t('place_holders.municipal_registration') %>
              </div>
              <div class="col-md-6">
                <strong><%= form.label :state_registration %></strong>
                <%= form.text_field :state_registration, class:"form-control form-control-user", placeholder:t('place_holders.state_registration') %>
              </div>
            </div>

            <div class="form-group">
              <strong><%= form.label :credits_package %></strong>
              <%= form.collection_select(:credits_package_id, @credit_packages, :id, :name, {:prompt => t('prompt.credits_package')}, { class:"form-control" }) %>
            </div>

            <div class="form-group custom-control custom-checkbox small ">
              <%= form.check_box :is_available, class:"custom-control-input" %>
              <%= form.label :is_available, class:"custom-control-label" %>
            </div>

          </div> <!-- .card-body-->
        </div><!-- . card Content - Collapse -->
      </div> <!-- .mb4-->
    </div> <!-- .Dealerships Information (lg-6) -->

    <div class="col-lg-6"> <!-- card Financial Settings -->
      <div class="card border-left-danger shadow mb-4">
        <a href="#collapseCardDealershipFinancialSettings" class="d-block card-header py-3" data-toggle="collapse" role="button" aria-expanded="true" aria-controls="collapseCardDealershipFinancialSettings"><h6 class="m-0 font-weight-bold text-primary"><%= t('labels.financial_settings') %></h6></a>
          <div class="collapse show" id="collapseCardDealershipFinancialSettings">
            <div class="card-body">
              <%= form.fields_for :dealerships_setting, @dealership.dealerships_setting do |dealerships_setting| %>
                <%= render partial: 'dealerships_setting_fields', locals: { f: dealerships_setting } %>
              <% end %>
            </div> <!--card-body -->
          </div> <!--collapse card dealership Financial settings -->
      </div><!--mb-4 -->
    </div> <!-- .Financial Settings (lg-6) -->

    <div class="col-lg-12"> <!-- card Addresses -->
      <div class="card border-left-danger shadow mb-4">
        <a href="#collapseCardAddressess" class="d-block card-header py-3" data-toggle="collapse" role="button" aria-expanded="true" aria-controls="collapseCardAddressess"><h6 class="m-0 font-weight-bold text-primary"><%= t('labels.addresses') %></h6></a>
          <div class="collapse show" id="collapseCardAddressess">
            <div class="card-body">
              <div id="addresses">
                <%= form.fields_for :addresses do |address| %>
                  <%= render partial: 'address_fields', locals: { f: address } %>
                <% end %>
                <%= link_to_add_association('Adicionar endereço', form, :addresses) %>
              </div> <!--  addresses -->
            </div> <!--card-body -->
          </div> <!--collapse card dealership Addressess -->
      </div> <!-- mb-4 -->
    </div>  <!--col-lg-12 -->

    <div class="col-lg-12"> <!-- card Machines-->
      <div class="card border-left-warning shadow mb-4">
        <a href="#collapseCardDealershipMachines" class="d-block card-header py-3" data-toggle="collapse" role="button" aria-expanded="true" aria-controls="collapseCardDealershipMachines"><h6 class="m-0 font-weight-bold text-primary"><%= t('labels.machines') %></h6></a>
          <div class="collapse show" id="collapseCardDealershipMachines">
            <div class="card-body">
              <div id="machines">
                <%= form.fields_for :machines do |machine| %>
                  <%= render partial: 'machine_fields', locals: { f: machine } %>
                <% end %>
                <%= link_to_add_association('Adicionar equipamento', form, :machines) %>
              </div> <!-- machines -->
            </div> <!--card-body -->
          </div> <!--collapse card machines -->
      </div><!--mb-4 -->
    </div>  <!--col-lg-12 -->

    <div class="col-lg-6"> <!-- card Operators-->
      <div class="card border-left-warning shadow mb-4">
        <a href="#collapseCardDealershipOperators" class="d-block card-header py-3" data-toggle="collapse" role="button" aria-expanded="true" aria-controls="collapseCardDealershipOperators"><h6 class="m-0 font-weight-bold text-primary"><%= t('labels.operators') %></h6></a>
          <div class="collapse show" id="collapseCardDealershipOperators">
            <div class="card-body">
              <div id="operators">
                <%= form.fields_for :operators do |operator| %>
                  <%= render partial: 'operator_fields', locals: { f: operator } %>
                <% end %>
                <%= link_to_add_association('Adicionar operador', form, :operators) %>
              </div> <!-- operators -->
            </div> <!--card-body -->
          </div> <!--collapse card dealership settings information -->
      </div><!--mb-4 -->
    </div>  <!--.Operators (lg-6) -->

    <div class="col-lg-6"> <!-- Card Cards =) -->
      <div class="card border-left-warning shadow mb-4">
        <a href="#collapseCardDealershipCards" class="d-block card-header py-3" data-toggle="collapse" role="button" aria-expanded="true" aria-controls="collapseCardDealershipCards"><h6 class="m-0 font-weight-bold text-primary"><%= t('labels.cards') %></h6></a>
        <div class="collapse show" id="collapseCardDealershipCards">
          <div class="card-body">
            <div id="cards">
              <%= form.fields_for :cards do |c| %>
                <%= render partial: 'card_fields', locals: { f: c } %>
              <% end %>
              <%= link_to_add_association('Adicionar cartão', form, :cards) %>
            </div> <!-- cards -->
          </div> <!--card-body -->
        </div> <!--collapse card dealership cards -->
      </div><!--mb-4 -->
    </div>  <!--.Cards -->

    <div class="actions">
      <div class="col-sm-4 mb-3 mb-sm-0 ">
        <%= form.submit t('buttons.save'), class:"btn btn-success btn-user"%>
      </div>
    </div> <!-- actions -->

  <% end %>
</div>

然后我可以保存一家经销店。布局由于某些我不了解的原因而损坏,但是我将让您解决(css类无法按我预期的方式运行)。

注意:这是我更喜欢(或建议)使用haml,slim,..之类的库的原因之一,因为您的视图代码将更易于阅读/维护并且是正确的。