将嵌套属性添加到现有对象

时间:2019-02-16 00:35:32

标签: ruby-on-rails

我正在使用宝石茧来嵌套属性

我有一个具有多种尺寸的产品模型。

创建产品及其尺寸效果很好

如果存在大小,我可以更新:size_name和/或:quantity属性。而且我可以破坏大小。很好...

但是我不能向现有产品添加更多尺寸。

是否有解决方案来增加现有产品的尺寸?

在产品模型中,我有:

has_many :sizes, inverse_of: :product, dependent: :destroy

accepts_nested_attributes_for :sizes, reject_if: :all_blank, allow_destroy: true

在我的products_controller.rb

   def update
    if @product.update_attributes(params_product)
        respond_to do |format|
            format.html {redirect_to admin_product_path(@product)}
            format.js
        end
    else
        render :edit
    end
   end

   def params_product
      params.require(:product).permit(
         :id, 
         :title, 
         :ref,
         :brand, 
         :description, 
         :buying_price, 
         :price,
         :category_id, 
         :color,
         { attachments:[]},
         sizes_attributes: [:id, :size_name, :quantity, :_destroy]
       )
   end

编辑表单

    <%= f.simple_fields_for :sizes do |size| %>
      <%= render "size_fields", f: size %>
    <% end %>
    <div class="links">
      <%= link_to_add_association "Ajouter une taille", f, :sizes, partial: "size_fields", class: "btn btn-secondary btn-lg" %>
    </div>

部分

<div class="nested-fields">
    <div class="row">
        <div class="col-12 col-md-4">
        <%= f.input :size_name, label: false, placeholder: "Taille", autofocus: true %>
        </div>
        <div class="col-12 col-md-4">
        <%= f.input :quantity, label: false, placeholder: "Quantité dans cette taille" %>
        </div>
        <div class="col-12 col-md-4">
            <%= link_to_remove_association "supprimer cette taille", f, class: "btn btn-danger delete_size" %>
            <hr>
        </div>
    </div>
</div>

如果您需要更多代码,请随时询问:)

更新

功能测试接受现有尺寸的更新,但Capybara找不到新字段来添加新尺寸...(WIP)

这是我向现有产品中添加新商品时的日志

Started PATCH "/admin/products/26" for 127.0.0.1 at 2019-02-16 11:31:51 +0100
  [1m[36mUser Load (0.4ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 23], ["LIMIT", 1]]
  ↳ /Users/johan/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by Admin::ProductsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xG3tRqzIC0XP4quanEo2589zbsSiJcwICXjIt9ecndDsKjwYIDSBVZ4P62woGNgnCYHhBiM2jXmZlQjz9CK2fQ==", "product"=>{"category_id"=>"8", "brand"=>"Side Park", "title"=>"Jean", "color"=>"Bleu", "price"=>"30.0", "buying_price"=>"5.0", "ref"=>"SP02", "description"=>"Paalablalntalon blab", "sizes_attributes"=>{"0"=>{"size_name"=>"L", "quantity"=>"3", "_destroy"=>"false", "id"=>"97"}, "1"=>{"size_name"=>"S", "quantity"=>"3", "_destroy"=>"false", "id"=>"95"}, "2"=>{"size_name"=>"M", "quantity"=>"4", "_destroy"=>"false", "id"=>"96"}}}, "commit"=>"Mettre à jour l'article", "id"=>"26"}
  [1m[36mProduct Load (0.3ms)[0m  [1m[34mSELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2[0m  [["id", 26], ["LIMIT", 1]]
  ↳ app/controllers/admin/products_controller.rb:62
  [1m[35m (0.1ms)[0m  [1m[35mBEGIN[0m
  ↳ app/controllers/admin/products_controller.rb:49
  [1m[36mSize Load (0.5ms)[0m  [1m[34mSELECT "sizes".* FROM "sizes" WHERE "sizes"."product_id" = $1 AND "sizes"."id" IN ($2, $3, $4)[0m  [["product_id", 26], ["id", 97], ["id", 95], ["id", 96]]
  ↳ app/controllers/admin/products_controller.rb:49
  [1m[35m (0.2ms)[0m  [1m[35mCOMMIT[0m
  ↳ app/controllers/admin/products_controller.rb:49
Redirected to http://localhost:3000/admin/products/26
Completed 302 Found in 13ms (ActiveRecord: 1.0ms)

1 个答案:

答案 0 :(得分:0)

我已将其添加到产品控制器中,现在可以为现有产品添加尺寸

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

def edit
    @product.sizes.build
end