在 rails 中使用 collection_check_boxes 填充表单时传递额外的属性以连接表? [英] Passing extra attributes to join table when populating the form with collection_check_boxes in rails?

查看:59
本文介绍了在 rails 中使用 collection_check_boxes 填充表单时传递额外的属性以连接表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 rails 中使用 collection_check_boxes 填充表单时,是否可以添加额外的属性以连接表,或者我是否必须以不同的方式填充表单?

目前我有 Product、Package 和 PackageItem 模型.包包含许多产品,但也可以在一个包中包含每个产品的多个实例(例如,一个包中可以有 2 个转盘).在我的表格中,我有:

<%= f.collection_check_boxes :product_ids, Product.all, :id, :name, {}, { :multiple =>真 } %>

并且在我的包控制器中我允许 product_ids =>[] 在强参数中.使用 package_items 表中的 2 个外键成功创建了连接,但是还有另一列 number 我希望在其中存储包中每个产品的编号.是否可以使用 collection_check_boxes 来执行此操作,或者我是否必须以不同的方式构建表单?

解决方案

根据要求,我就是这样解决这个问题的(使用数字输入字段).

表格相关部分:

<% @products.each 做 |product|%><div class="col-xs-4 字段"><%= hidden_​​field_tag 'product[][id]', product.id %><%= label_tag 'product[][name]', product.name %><%= number_field_tag 'product[][quantity]', {}, id: "product_" + product.id.to_s %>

<%结束%>

还有我的包控制器代码(注意我手动填充字段以便能够添加非连接参数......不确定是否有更多的 rails 方法来做到这一点?).也不确定我的解决方案的安全性,因为我没有将 product[:quantity] 包含在控制器底部私有部分的强参数中(不记得我的理性了).从理论上讲,这意味着有人可以在数据库中插入恶意代码,但这不太可能,因为它是一个整数字段,并且只能由破坏自己的管理员进行编辑.如果您要发布一个消费者应用程序,我会仔细检查一下它的潜在影响:

 def 创建@package = Package.new(package_params)@package_items = params[:product].delete_if { |product|!product[:quantity].present?||(产品[:数量]).to_i <= 0}如果@package.save@package_items.each 做 |x|packageitem = PackageItem.create(package_id: @package.id, product_id: x['id'], 数量: x['quantity'])packageitem.save!结尾response_to do |格式|flash.now[:notice] = "包创建成功!"format.html { redirect_to packages_path }format.json { 头:no_content }格式.js结尾结尾结尾私人的def set_products@products = Product.all结尾def package_paramsparams.require(:package).permit(:name,:price,:package_image,:multiple_day_discount)结尾

和模型/package_item.rb

class PackageItem 

Is it possible to add extra attributes to join table when populating form with collection_check_boxes in rails or do I have to populate the form in a different manner?

Currently I have Product, Package and PackageItem models. Packages contain many products but can also have multiple instances of each product in a package (eg a package could have 2 turntables in it). In my form I have:

<%= f.collection_check_boxes :product_ids, Product.all, :id, :name, {}, { :multiple => true } %>

and in my packages controller I allow product_ids => [] in strong params. The join is being successfully created with the 2 foreign keys in the package_items table however there is another column number where I wish to store the number of each product in a package. Is it possible to do this using collection_check_boxes or do I have to build my form in a different way?

解决方案

As requested, this is how I solved this issue (using a number input field).

Relevant section of form:

<div id="package_products" class="col-xs-8">
  <% @products.each do |product| %>
    <div class="col-xs-4 field">
      <%= hidden_field_tag 'product[][id]', product.id %>
      <%= label_tag 'product[][name]', product.name %>
      <%= number_field_tag 'product[][quantity]', {}, id: "product_" + product.id.to_s %>
    </div>
  <% end %>
</div>

And my packages controller code (notice I manually populate the fields as to be able to add the non-join params... not sure if there is a more rails way to do this?). Also not sure about the security of my solution as I have not included product[:quantity] in strong params in the private section at the bottom of the controller (can't remember my rational). Theoretically this means that someone could insert malicious code in the db but it's highly unlikely as it's an integer field and it was only editable by admins who would be sabotaging themselves. If you are releasing a consumer app into the wild I would double check the potential repercussions of this:

 def create
    @package = Package.new(package_params)
    @package_items = params[:product].delete_if { |product| !product[:quantity].present? || (product[:quantity]).to_i <= 0}
    if @package.save 
      @package_items.each do |x|
        packageitem = PackageItem.create(package_id: @package.id, product_id: x['id'], quantity: x['quantity'])
        packageitem.save!
      end
        respond_to do |format|
           flash.now[:notice] = "Package was successfully created!"
            format.html { redirect_to packages_path }
            format.json { head :no_content }
            format.js
        end
    end
  end



private

    def set_products
      @products = Product.all
    end

    def package_params
      params.require(:package).permit(:name, :price, :package_image, :multiple_day_discount)
    end

And model/package_item.rb

class PackageItem < ActiveRecord::Base
    belongs_to :package
    belongs_to :product
end

这篇关于在 rails 中使用 collection_check_boxes 填充表单时传递额外的属性以连接表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆