验证失败:库存产品必须存在,库存位置必须存在

时间:2016-07-11 17:01:45

标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5

我有三个型号 - >地点,产品和库存。

股票是地点和产品的连接表。

在创建新产品时,我使用fields_for来显示位置,即使我让它工作,由于某种原因现在它似乎不再起作用,它给了我上述错误。

<div class="input-field">
  <%= f.label :product_name %>
  <%= f.text_field :name, autofocus: true %>
</div>

<div class="input-field">
  <%= f.label :price %>
  <%= f.text_field :price, autofocus: true %>
</div>

<% if !@edit %>
  <%= f.fields_for :stocks do |ff| %>

    <div class="input-field  margin-top x-4">
      <%= ff.collection_select :location_id, Location.all, :id, :structured_location , {:prompt => "Please Select Locations for Product"}, {multiple: true} %>
      <%= ff.label :locations %>
    </div>

    <div class="input-field">
      <%= ff.label :quantity %>
      <%= ff.text_field :quantity %>
    </div>

    <div class="input-field">
      <%= ff.label :threshold_quantity %>
      <%= ff.text_field :threshold_quantity %>
    </div>

  <% end %>
<% else %>

      <%= collection_select :product, :location_ids, Location.all, :id, :structured_location , {:prompt => "Please Select Locations for Product"}, {multiple: true} %>


<% end %>

<div class="row margin-top x-4">
  <div class="col s12 center-align">
    <%= f.submit "#{current_page?(new_product_path) ? "Create Product" : "Update Product"}", class: "btn wave-effect pink darken-1 btn-large" %>
  </div>
</div>

控制器

class ProductsController < ApplicationController
  helper_method :sort_column, :sort_direction
  def index
    @products = Product.order(sort_column + " " + sort_direction)
  end

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

  def create
    @product = Product.new(product_params)

    if @product.save!
      flash[:notice] = "Successfully saved..."
      redirect_to products_path
    else
      flash[:alert] = "Something went wrong, please check the values you entered"
      redirect_to :back
    end
  end

private

  def product_params
    params.require(:product).permit(:name,:price, location_ids: [], stocks_attributes: [:id, :quantity, :threshold_quantity, location_id: []])
  end
end

产品型号

class Product < ApplicationRecord
  has_many :stocks, dependent: :destroy
  has_many :locations, :through => :stocks

  accepts_nested_attributes_for :stocks
end
rails console中的

参数

  

参数:{“utf8”=&gt;“✓”,   “authenticity_token”=&gt; “中l1BFhrdyB2QMO5k3 + 60GNiPphFfF + DXDGPbUU3V2Op2aekObjgIe13k8uoedmDIEZgIeXPZUeS / 0VxQXkKa1Uw ==”,   “product”=&gt; {“name”=&gt;“肥皂”,“价格”=&gt;“10”,“location_ids”=&gt; [“”,“1”,   “2”],“stocks_attributes”=&gt; {“0”=&gt; {“数量”=&gt;“100”,   “threshold_quantity”=&gt;“100”}}},“commit”=&gt;“创建产品”}

1 个答案:

答案 0 :(得分:3)

After hours of searching i stumbled upon this post

BigBinary

it seems that Rails 5 made belongs_to relationship IDs required by default thats why i had validations failed and i couldn't find anything about it.

simply adding optional: true in my stock model worked!

class Stock < ApplicationRecord
  belongs_to :location, optional: true
  belongs_to :product, optional: true
  accepts_nested_attributes_for :product, allow_destroy: true
  accepts_nested_attributes_for :location, allow_destroy: true

end