如何使用茧宝石使用具有不同关系的嵌套形式?
Posted
技术标签:
【中文标题】如何使用茧宝石使用具有不同关系的嵌套形式?【英文标题】:How to use nested forms with different relations using the cocoon gem? 【发布时间】:2019-09-24 11:59:48 【问题描述】:我的困境已经持续了一周,我正在努力解决它,但到目前为止我还没有解决,所以我正在寻求帮助。
我有一个模型经销商和其他嵌套(DealershipsSetting、地址、机器、操作员、卡),其中 has_one 关系仅适用于 DealershipsSetting,因此其他是 has_many。 我只能在数据库表中写入一些信息,当我为嵌套表单的每个模型调用构建方法时,例如:
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) 我在日志中注意到的第一件事是表单被重定向了多次。这是有道理的,因为我强迫他们通过 builds 调用来构建(至少我是这么理解的)。但这不是使用 gem 时的默认行为,我在另一个应用程序中进行了测试,只是为了了解如何使用 gem,遵循文档https://github.com/nathanvda/cocoon
b) 由于调用了build方法,表单打开(准备接收数据),不知道是不是默认行为。
c) 当我调用 Action New 或 Edit 时,我不能记录多个记录(类型、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
模型经销商.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
型号地址.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 - 管理经销商,他们必须有一种配置和一个或多个地址、机器、操作员和卡。
我会非常感谢任何可以帮助我的人。
【问题讨论】:
我知道这没什么用,但有时我发现嵌套属性不起作用。但只有有时。所以我创建了辅助方法来循环并创建相关的记录。我坚持使用fields_for
,但有时,它对我不起作用:(
非常感谢@MikeHeft 我仍然希望有一个解决方案,我认为它可能存在。 ;)
请解释一下:如果你说“添加一个寄存器”,你的意思是保存一个记录吗?您能否显示日志文件,表单重定向不应该由“构建”(它只是预先创建数据)引起,并且日志文件希望向我们展示更多信息,为什么不保存记录?我很好奇参数是如何发布的,强参数定义必须正确匹配。
嗨,@nathanvda 非常感谢您的帮助。首先,我想说我是 web 开发的初学者,我认为是可以感知的 :) 所以,如果你可以检查一下,我做了一个要点,这是链接 gist.github.com/jonaslucena/b7f44bd17d5b3f35705aa94633a1ef31 关于你的问题,对不起我的穷人英语,我会尽量回答得更好。是的,我想说“保存新记录”。关于我明天可以发送的日志文件,好吗?感谢您的支持,非常感谢!最好的问候
代码对我来说一切都很好(但它很多,所以我当然可以忽略错误)。我能想到的唯一事情:验证失败或强参数阻塞参数。我期待看到日志文件。否则如果你有一个 github-repo 会重现这个问题,我会看看。
【参考方案1】:
好的,所以我遇到了两个主要错误:
您的表单标签位于一个 div 中,但您的表单内容跨越了多个 div。它看起来是正确的,但不会在所有情况下都发布正确的信息 其次:在我的情况下,您确实有阻止保存的验证,然后发生了两件事: 您没有在create
操作中设置@credit_packages
,导致重新呈现表单时出错
您没有显示任何验证错误(因此没有关于保存失败原因的反馈)
所以我做了两件事。在您的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 等库的原因之一,因为您的视图代码将更易于阅读/维护并且是正确的。
【讨论】:
非常感谢您的帮助,我将开始学习slim和haml,也感谢您的建议。最好的问候o/以上是关于如何使用茧宝石使用具有不同关系的嵌套形式?的主要内容,如果未能解决你的问题,请参考以下文章