Создать объект с помощью вложенных форм - PullRequest
0 голосов
/ 16 мая 2018

В моем проекте есть следующие таблицы:

A subset of my database tables

Я пытаюсь создать вложенную форму, чтобы создать Модель продукта , в которой пользователь может ввести имя и проверить, какие Attr Vals этот новый Модель продукта будет иметь.

Вот что у меня есть:

attr.rb

class Attr < ApplicationRecord
  validates :name, presence: true
  validates :name, uniqueness: true

  has_many :attr_vals
end

attr_val.rb

class AttrVal < ApplicationRecord
  validates :name, presence: true
  validates :name, uniqueness: { scope: :attr_id }

  has_many :product_models, through: :product_model_attr_vals
  has_many :products, through: :product_attr_vals

  belongs_to :attr
end

product_model_attr_val.rb

class ProductModelAttrVal < ApplicationRecord
  validates :product_model_id, uniqueness: { scope: :attr_val_id }

  belongs_to :product_model
  belongs_to :attr_val
end

product_model.rb

class ProductModel < ApplicationRecord
  validates :name, presence: true
  validates :name, uniqueness: true

  has_many :products
  has_many :product_model_attr_vals
  has_many :attr_vals, through: :product_model_attr_vals
  has_many :attrs, through: :attr_vals

  accepts_nested_attributes_for :product_model_attr_vals

  belongs_to :product_category
end

Код продукта / new.html.erb

<%= form_for @product_model, url: {action: "create"} do |pm_f| %>
  <%= pm_f.text_field :name %><br />
  <br />
  <ul>
    <% Attr.all.each do |attr| %>
      <li>
        <%= label_tag attr.name %><br />
          <ul>
            <% attr.attr_vals.each do |attr_val| %>
              <li>
                <%= pm_f.fields_for :product_model_attr_vals, attr_val, index: attr_val.id do |av_f| %>
                  <%= av_f.check_box :id, { checked: @product_model.attr_vals.where(id: attr_val.id).count > 0 } %>
                  <%= label_tag attr_val.name %><br />
                <% end %>
              </li>
            <% end %>
          </ul>
      </li>
    <% end %>
  </ul>
  <br/>
  <%= pm_f.submit "Create" %>
<% end %>

product_model_controller.rb

def create
  if ProductModel.create(product_model_params)
    # do nothing
  else 
    @product_model = ProductModel.new
    @attrs = Attr.all # delete me
    @categories = ProductCategory.all #delete me
  end
  render 'new'
end

def new
    @product_model = ProductModel.new
    @attrs = Attr.all
    @categories = ProductCategory.all
end

private

def product_model_params
  params.require(:product_model).permit(:name, product_model_attr_vals_attributes: [:id, :name])
end

Результат примерно такой:

Form

У меня проблема в том, что Модель продукта не создается из-за некоторых ошибок, и я не знаю, как их исправить, так как я новичок в Ruby on Rails и не знаю, где еще искать помощь .

Чего мне не хватает?


Processing by ProductModelsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"f56GFgd99m54gAbhn7iPuLLs8oAqsRZDtK58gqLlW8GP3kXs436ozCBkG+745cDF847gDCcEg==", "product_model"=>{"name"=>"New Product Model Name", "product_model_attr_vals_attributes"=>{"0"=>{"1"=>{"id"=>"1"}}, "1"=>{"2"=>{"id"=>"2"}}, "2"=>{"3"=>{"id"=>"3"}}, "3"=>{"4"=>{"id"=>"4"}}, "4"=>{"5"=>{"id"=>"5"}}, "5"=>{"6"=>{"id"=>"6"}}, "6"=>{"7"=>{"id"=>"7"}}, "7"=>{"8"=>{"id"=>"8"}}, "8"=>{"9"=>{"id"=>"9"}}, "9"=>{"10"=>{"id"=>"10"}}, "10"=>{"11"=>{"id"=>"11"}}}}, "commit"=>"Create"}
Unpermitted parameter: :1
Unpermitted parameter: :2
Unpermitted parameter: :3
Unpermitted parameter: :4
Unpermitted parameter: :5
Unpermitted parameter: :6
Unpermitted parameter: :7
Unpermitted parameter: :8
Unpermitted parameter: :9
Unpermitted parameter: :10
Unpermitted parameter: :11
   (0.1ms)  BEGIN
  ProductModel Exists (0.2ms)  SELECT  1 AS one FROM `product_models` WHERE `product_models`.`name` = BINARY 'New Product Model Name' LIMIT 1
  ProductModelAttrVal Exists (0.2ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
  CACHE ProductModelAttrVal Exists (0.0ms)  SELECT  1 AS one FROM `product_model_attr_vals` WHERE `product_model_attr_vals`.`product_model_id` IS NULL AND `product_model_attr_vals`.`attr_val_id` IS NULL LIMIT 1  [["LIMIT", 1]]
   (0.1ms)  ROLLBACK
  Rendering product_models/new.html.erb within layouts/application
  Rendered product_models/new.html.erb within layouts/application (1.7ms)
Completed 500 Internal Server Error in 20ms (ActiveRecord: 0.7ms)
...