如何通过Rails中的嵌套表单传递account_id

时间:2019-05-29 16:52:16

标签: ruby-on-rails ruby forms nested-forms

我正在尝试在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

正确的方法是什么?

2 个答案:

答案 0 :(得分:2)

或者,您也可以传递一个带有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

答案 1 :(得分:0)

最简单的选择是将隐藏字段添加到两种形式:

https://apidock.com/rails/ActionView/Helpers/FormHelper/hidden_field

对于您来说,对于选项表,类似:

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

这将使您可以访问[:option][:account_id]参数,该参数将与当前用户的参数匹配。