【发布时间】:2018-12-13 05:31:36
【问题描述】:
我有一个表格。 When a category is selected that does not have any subcategories, the default is 0 when ADDED by the database.但是当它被编辑时,如果不存在子类别,则值不会改变。
如果子类别表单字段不存在,我有一个隐藏子类别表单字段的咖啡脚本。
例如,我可能有两个类别:
零件
配件
零件类别可能有两个子类别,例如轮辋和轮胎。但配件类别可能根本没有子类别。
<div class="form-group">
<%= f.label "Category" %>
<%= f.collection_select(:product_category_id, ProductCategory.all, :id, :name) %>
</div>
<div class="form-group">
<%= f.label :product_subcategory_id, "Subcategory", class: "control-label" %>
<%= f.grouped_collection_select(:product_subcategory_id, ProductCategory.order(:name), :product_subcategories, :name, :id, :name) %>
</div>
如果没有选择子类别,我希望 product_subcategory_id 的值设置为 0。
更新:prodcut_controller.rb
def update
@product = Product.find(params[:id])
if params.has_key?(:product_subcategory_id)
else
params[:product_subcategory_id] = 0
end
if @product.update_attributes(product_params)
flash[:success] = "Updated successfully."
redirect_to admin_products_path
else
render 'edit'
end
end
【问题讨论】:
标签: ruby-on-rails