mardi 8 décembre 2015

Creating Dynamic Form That will Create 2 Models Using Form Object

Hello i have 3 models Product, Category, and ProductSize.

In my Product New View the user should be able to pick what Category(T-Shirt) the product is then a list of sizes(XS, S, M, L, XL) for that Category will drop down then the user can input the quantity of each size they have. Then they fill out the rest of the fields and click create.

Problem is when the user clicks create the params that are getting passed to my ProductsController Create action doesn't contain the size_id or the quantity. How do i get the view to pass the correct params that will include the the quantity for each size_id?

The way i'm creating both a Product and ProductSize is by a form object. If i can get the correct params to the create action i will be able to do the rest. I just can't figure out how to get the quantity for each size_id in the params so i can create a Product and ProductSize.

I think there is something wrong with my ProductController New action and my form. I never created a form like this before so its very new to me.

Please see where it says product_size

   35: def create
 => 36:   binding.pry
    37:   @product_form = ProductForm.new(product_params)
    38:   if @product_form.save
    39:     redirect_to @product
    40:     flash[:success] = "You have created a new product"
    41:   else
    42:     flash[:danger] = "Your product didn't save"
    43:     render "new"
    44:   end
    45: end

[1] pry(#<ProductsController>)> params
=> {"utf8"=>"✓",
 "authenticity_token"=>"QrORHJ5DsdidyoUDgMcCDO26/Uk9Uuq8N40FEqv240PYfZUKBh9Nirt25kimEorQ4luXqNKxKXoG4zqqGiLtdA==",
 "product"=>
  {"image"=>
    #<ActionDispatch::Http::UploadedFile:0x007fdaccb89120
     @content_type="image/jpeg",
     @headers="Content-Disposition: form-data; name=\"product[image]\"; filename=\"David-Marvier-Beauty-Fashion-Photography17.jpg\"\r\nContent-Type: image/jpeg\r\n",
     @original_filename="David-Marvier-Beauty-Fashion-Photography17.jpg",
     @tempfile=#<File:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151208-24784-1tgqy4v.jpg>>,
   "category_id"=>"3",
   "title"=>"Test",
   "price"=>"12",
   "description"=>"test",
   "tag_list"=>"test"},
 "product_size"=>{"quantity"=>""},
 "commit"=>"Create new product",
 "controller"=>"products",
 "action"=>"create"}

Here is my form

<%= javascript_include_tag "custom" %>
<div class="container">
  <div class=“row”>
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-primary">
        <div class="panel-body">
          <%= simple_form_for @product_form, html: { multipart: true }, method: :post do |f| %>
            <%= f.input :image, label:"Choose Image" %>
            <%= f.collection_select :category_id, @categories, :id, :name, include_blank: true, :prompt => "Select One Category" %>
            <% @categories.each do |category| %>
              <div class='sizes_container' id ='sizes_container_for_<%= category.id %>'>
                <% category.sizes.each do |size| %>
                  <%= size.title %>
                  <%= simple_fields_for @product_size do |f| %>
                    <%= f.input :quantity %>
                  <% end %>
                <% end %>
              </div>
            <% end %>
            <%= f.input :title, label:"Title"%>
            <%= f.input :price, label:"Price"%>
            <%= f.input :description,label:"Description" %>
            <%= f.input :tag_list,label:"Tags - Seperate tags using comma ','. 5 tags allowed per product" %>
            <%= f.button :submit, "Create new product", class: "btn-lg btn-success" %>
          <% end %>
        </div>
      </div>
    </div>
  </div>
</div>

Here is my Products Controller.

class ProductsController < ApplicationController

  def new
    @product_form = Product.new
    @categories = Category.preload(:sizes).order(:name)
    @product_size = ProductSize.new
  end

  def create
    @product_form = ProductForm.new(product_params)
    if @product_form.save
      redirect_to @product
      flash[:success] = "You have created a new product"
    else
      flash[:danger] = "Your product didn't save"
      render "new"
    end
  end

  private

  def product_params
    params.require(:product).permit(
      :title,
      :price,
      :description,
      :tag_list,
      :category_id,
      :size,
      :quantity
    )
  end
end

Product.rb

class Product < ActiveRecord::Base
  acts_as_taggable

    belongs_to :user
    belongs_to :category

  has_many :product_sizes

    validates :title, presence: true, length: { maximum: 30 }
    validates :description, presence: true, length: { maximum: 2000 }
    validates :category, :user_id, :price, presence: true

    has_attached_file :image, styles: { large: "600x600", medium: "250x250", thumb:"100x100#"}
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

ProductSize.rb

class ProductSize < ActiveRecord::Base
  belongs_to :user
  belongs_to :size

  validates :quantity, presence: true
end

Aucun commentaire:

Enregistrer un commentaire