Rails通过select2下拉参数有很多问题

问题描述

我有一个select2选择字段,我正在选择产品附带的选项类型。我通过product_option_types在产品和option_types之间有很多联系。我正在尝试以逗号分隔的select2结果,并为preoduct_option_types创建联接表记录。到目前为止,它仍然无法正常工作。

我收到产品错误未知属性'option_type_id'。以下是详细信息。有关如何执行此操作的任何想法?

产品型号

class Product < ApplicationRecord
  belongs_to :user
  belongs_to :category
  has_one_attached :main_image,dependent: :destroy
  has_many_attached :images,dependent: :destroy

  has_many :product_option_types,dependent: :destroy
  has_many :option_types,through: :product_option_types


  has_many :variants,inverse_of: :product

end

option_types模型:

class OptionType < ApplicationRecord
  with_options dependent: :destroy,inverse_of: :option_type do
    has_many :option_values,-> { order(:position) }
    has_many :product_option_types
  end

  accepts_nested_attributes_for :option_values,reject_if: :all_blank,allow_destroy: true

  has_many :products,through: :product_option_types
end

product_option_types模型:

class ProductOptionType < ApplicationRecord
  with_options inverse_of: :product_option_types do
      belongs_to :product
      belongs_to :option_type
  end

  validates :product,:option_type,presence: true
  validates :product_id,uniqueness: { scope: :option_type_id },allow_nil: true
end

产品_form

<%= form_with(model: product,local: true) do |form| %>
  <div class="row">
    <div class="col-md-9">

  <% if product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(product.errors.count,"error") %> prohibited this product from being saved:</h2>

      <ul>
      <% product.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= form.label :name %>
    <%= form.text_field :name,class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= form.label :description %>
    <%= form.text_area :description,class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= form.label :stock %>
    <%= form.text_field :stock,class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= form.label :available_on %>
    <%= form.text_field :available_on,class: 'form-control',data: { behavior: "flatpickr" } %>
  </div>

  <div class="form-group">
    <%= form.label :price %>
    <%= form.text_field :price,class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= form.label 'Category' %>
  <%= select_tag(:category_id,options_for_select(@categories,u/product.category_id),:prompt => 'Select category') %>
  </div>

  <div class="form-group" data-controller='select2'>
    <%= form.label :option_type_id %>

    <%= form.select :option_type_id,OptionType.all.map { |type| type.presentation },{include_blank: false},class: 'form-control content-search',multiple: 'multiple' %>

  </div>

    <hr>
    <div class="form-group">
    <%= form.submit class: 'btn btn-primary' %>

    <% if product.persisted? %>
    <div class="float-right">
      <%= link_to 'Destroy',product,method: :delete,class: "text-danger",data: { confirm: 'Are you sure?' } %>
    </div>
      <%= link_to "Cancel",class: "btn btn-link" %>
    <% else %>
      <%= link_to "Cancel",products_path,class: "btn btn-link" %>
    <% end %>

  </div>
</div>

<div class="col-md-3">
  <div class="form-group">
    <%= form.label 'Main Product Image' %>
    <%= form.file_field :main_image,classs: 'form-control' %>
  </div>

  <% if product.persisted? %>

      <div class="form-group">
          <%= link_to "Manage Variants",product_variants_path(@product),class: "btn btn-link" %>
      </div>

    </div>
    <% end %>
  </div>
  <% end %>

产品控制器:

class ProductsController < ApplicationController
  before_action :set_product,only: [:show,:edit,:update,:destroy]
  before_action :load_data
  # GET /products
  # GET /products.json
  def index
    u/products = Product.all
    u/option_types = OptionType.all.map{|c| [ c.name,c.id ] }
  end

  # GET /products/1
  # GET /products/1.json
  def show
    puts params.inspect
  end

  # GET /products/new
  def new
    u/product = Product.new
    u/product.option_types.new
    u/categories = Category.all.map{|c| [ c.title,c.id ] }
    u/variants = Variant.all.map{|c| [ c.name,c.id ] }
  end

  # GET /products/1/edit
  def edit
    u/categories = Category.all.map{|c| [ c.title,c.id ] }
    u/option_types = u/product.option_types
    u/product.option_types.new
  end

  # POST /products
  # POST /products.json
  def create
    u/product = Product.new(product_params)
    u/product.user_id = current_user.id
    u/product.category_id = params[:category_id]
    respond_to do |format|
      if u/product.save
        format.html { redirect_to u/product,notice: 'Product was successfully created.' }
        format.json { render :show,status: :created,location: u/product }
      else
        format.html { render :new }
        format.json { render json: u/product.errors,status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    u/product.category_id = params[:category_id]

    if u/product.option_types.present?
      u/product.option_types = u/product.option_types.split(',')
    end

    respond_to do |format|
      if u/product.update(product_params)
        format.html { redirect_to u/product,notice: 'Product was successfully updated.' }
        format.json { render :show,status: :ok,location: u/product }
      else
        format.html { render :edit }
        format.json { render json: u/product.errors,status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    u/product.destroy
    respond_to do |format|
      format.html { redirect_to products_url,notice: 'Product was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      u/product = Product.find(params[:id])
    end

    def load_data
      u/option_types = OptionType.order(:name)
    end

    # Only allow a list of trusted parameters through.
    def product_params
      params.require(:product).permit(:name,:description,:stock,:available_on,:price,:user_id,:main_image,:option_type_ids,:option_type_id: [])
    end

解决方法

我知道了。我没有在选择字段中传递ID。我将其更改为此,并且可以正常工作。

  <%= form.select :option_type_ids,OptionType.all.map { |type| [type.presentation,type.id] },{prompt: true},class: 'form-control content-search',multiple: 'multiple' %>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...