【发布时间】:2020-03-28 03:53:06
【问题描述】:
我有一个用于新建和编辑的表单。表格中包含产品名称、描述等内容。
此外,用户可以从嵌套下拉列表 (collection_select) 中选择一个项目,也可以创建一个新项目。在“新”表单上工作正常 - 所有条目和选择都保存。
当用户编辑保存的产品时,表单会预加载该项目的所有已填写条目,但不会在 collection_select 中预加载他们的原始选择。
并且,如果用户想要编辑该项目并决定创建一个新项目而不是先前选择的 collection_select 项目,则会出现错误,说明该产品已使用该化学组创建。 对于这种双重困境的任何帮助将不胜感激。我是 RoR 的新手,我确定我在某处遗漏了一些东西。
这是我的表格
<%= render partial: 'layouts/errors', locals: {object: @product} %>
<%= form_for(@product) do |f| %>
<div>
<%= f.label :name %><br>
<%= f.text_field :name %><br>
<div>
<div>
<%= f.label :active_ingredient %><br>
<%= f.text_field :active_ingredient %><br>
<div>
<div>
<%= f.label :description %><br>
<%= f.text_area :description %><br>
</div>
<div>
<%= f.label :image %><br>
<%= f.file_field :image %><br>
</div>
<div>
<p>Select a Chemical Group:</p>
<%= f.collection_select :chem_group_id, ChemGroup.all, :id, :name, include_blank: 'Select One', selected: @product.chem_group, value: @product.chem_group.name %>
</div>
<div>
<p>Or, create a new Chemical Group:</p>
<!-- NESTED FORM! User writing attributes for another object. Use fields_for -->
<%= f.fields_for :chem_group do |cg| %>
<%= cg.label :name %>
<%= cg.text_field :name %>
<% end %>
</div>
<div>
<p>Select an Application Area:</p>
<%= f.collection_select :application_area_id, ApplicationArea.all, :id, :area_name, include_blank: 'Select One', selected: @product.application_area, value: @product.application_area.area_name %>
</div>
<div>
<p>Or, create a new Application Area:</p>
<!-- NESTED FORM! User writing attributes for another object. Use fields_for -->
<%= f.fields_for :application_area do |aa| %>
<%= aa.label :area_name %>
<%=aa.text_field :area_name %>
<% end %>
</div>
<br>
<%= f.submit "Save" %>
<% end %>
这是我的模型
class Product < ApplicationRecord
belongs_to :chem_group
belongs_to :application_area
belongs_to :user #admin creator
accepts_nested_attributes_for :chem_group #tells the model to accept chem_group attributes from cg nested form in new product form
accepts_nested_attributes_for :application_area
validates :active_ingredient, presence: true
validates :application_area, presence: true
validates :description, presence: true
validates :name, presence: true
validate :not_a_duplicate #checking for what we DON'T WANT
def chem_group_attributes=(attributes)
self.chem_group = ChemGroup.find_or_create_by(attributes) if !attributes['name'].empty?
self.chem_group
end
def application_area_attributes=(attributes)
self.application_area = ApplicationArea.find_or_create_by(attributes) if !attributes['area_name'].empty?
self.application_area
end
#if there is already a product with that name && chem_group, give error
def not_a_duplicate
#calling the instance of the attribute [string/integer: key]
if Product.find_by(name: name, chem_group_id: chem_group_id)
errors.add(:name, 'has already been created for that Chemical Group')
end
end
end
这是我的控制器
class ProductsController < ApplicationController
def new
if logged_in?
@product = Product.new
1.times {@product.build_chem_group} #for the nested form. Builds the chem_group attributes
@product.build_application_area
else
flash[:error] = "Sorry, you must be logged in to create a new product."
redirect_to products_path
end
end
def create
@product = Product.new(product_params)
@product.user_id = session[:user_id] #bc product belongs_to user. user_id required from model
if @product.save #validation
# @product.image.purge
# @product.image.attach(params[:product][:image]) # allows image to be replaced if user changes image
redirect_to product_path(@product)
else
@product.build_chem_group
@product.build_application_area
render :new
end
end
def edit
find_product
1.times {@product.build_chem_group}
if @product.user != current_user
flash[:error] = "Sorry, you can only edit your own products"
redirect_to products_path
end
end
def update
find_product
if @product.update(product_params)
redirect_to product_path(@product)
else
render :edit
end
end
private
def product_params
params.require(:product).permit(:name, :description, :active_ingredient, :image, :chem_group_id, :application_area_id, chem_group_attributes: [:id, :name], application_area_attributes: [:id, :area_name])
#chem_group_id and chem_group_attributes [:name] is permitting elements from new product form
end
def find_product
@product = Product.find_by(id: params[:id])
end
end
【问题讨论】:
-
如果从
collection_select中删除selected和value选项会怎样?
标签: ruby-on-rails nested-form-for collection-select