首页 > 解决方案 > 如何通过 Rails 中的嵌套表单传递 account_id

问题描述

我正在尝试在 rails 中设置嵌套表单,并且表单中的父对象和子对象都需要有一个“帐户 ID”,以便它们都适用于当前用户的帐户,但我不知道如何通过嵌套表单为子对象传递当前用户的帐户 ID。我不断收到嵌套对象的“必须存在帐户 ID”的验证错误。

父表单是“产品”,我正在尝试将“选项”嵌套到 Product.new 表单中。

我正在尝试做这样的事情:

@product.options.account_id = current_user.account.id

但它不起作用。

这是产品型号:

class Product < ApplicationRecord
  belongs_to :account
  has_many :options, dependent: :destroy

  accepts_nested_attributes_for :options, allow_destroy: true

  validates :account_id,  presence: true
  validates :name,        presence: true, length: { maximum: 120 }
end

和选项模型:

class Option < ApplicationRecord
  belongs_to :account
  belongs_to :product

  has_many :option_values, dependent: :destroy

  validates :account_id,  presence: true
  validates :name,        presence: true,
                          length: { maximum: 60 }
end

这是我将“选项”嵌套到产品表单中的方式:

<%= form.fields_for :options do |builder| %>
    <fieldset class='form-group'>
      <%= builder.label :name, 'Add option(s)' %>
      <%= builder.text_field :name %>
      <small id="optionHelp" class="form-text text-muted">
        (e.g. "Sizes" or "Color")
      </small>
    </fieldset>
  <% end %>

这是我的 ProductsController:

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  before_action :restrict_access

  def index
    @products = Product.where(:account_id => current_user.account.id).all
  end

  def show
  end

  def new
    @product = Product.new
    @product.options.build
  end

  def edit
  end

  def create
    @account = current_user.account
    @product = @account.products.build(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
    end
  end

  private
    def set_product
      if Product.find(params[:id]).account_id == current_user.account.id
        @product = Product.find(params[:id])
      else
        redirect_to dashboard_path
      end
    end

    def restrict_access
      if index
        authorize @products
      else
        authorize @product
      end
    end

    def product_params
      params.require(:product).permit(:account_id, :name,
                                      options_attributes: [:id, :account_id, :name ])
    end
end

这样做的正确方法是什么?

标签: ruby-on-railsrubyformsnested-forms

解决方案


或者,您可以传递带有 form 和 nested_form 的 hidden_​​field,如下所示: -

<%= form_for @product do |form|%>
  <%= form.fields_for :options do |builder| %>
      <fieldset class='form-group'>
        <%= builder.label :name, 'Add option(s)' %>
        <%= builder.text_field :name %>
        <small id="optionHelp" class="form-text text-muted">
          (e.g. "Sizes" or "Color")
        </small>
        <%=builder.hidden_field :account_id, value: current_user.account.id%>
      </fieldset>
  <% end %>
  <%= form.hidden_field :account_id, value: current_user.account.id%>
<%end%>

除此之外,您可以在控制器上设置 account_id

  def new
    #@product = Product.new
    @product = current_user.account.products.new
    @product.options.build(account_id: current_user.account.id)
  end

推荐阅读