mardi 8 décembre 2015

Form Not Passing id and value to create a Form Object

Hello i have a form which is below. 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 my quantity and size_id isn't being passed in the params. How do i get this to send the quantity for each size. The params will need to show the quantity and size_id together. Some along the lines of {size_id: 21, quantity: 3} for each size with a quantity.

<%= 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 do |f| %>
            <%= 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 %>
                  <%= f.input :quantity %>
                <% 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 what the params look like when they are passed to the create action

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

[1] pry(#<ProductsController>)> params
=> {"utf8"=>"✓",
 "authenticity_token"=>"61eEnAhR6ZTqL1Z9/NYMBupZa7xDirdoOTQGUka+lOFxmYCKkA0VxsyTNTbaA4Ta5bgBXaxpdK4IWjnq92qa1g==",
 "product"=>{"category_id"=>"3", "quantities"=>"", "title"=>"testtest", "price"=>"123", "description"=>"test", "tag_list"=>"test"},
 "commit"=>"Create new product",
 "controller"=>"products",
 "action"=>"create"}

Product Controller new and create action.

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

  def create
    binding.pry
    @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

Here is the form object. Not finished yet. I need the params to work properly first.

class ProductForm
  include SimpleFormObject


  #Attributes for Product
  # attribute :image, :string
  attribute :title, :string
  attribute :price, :integer
  attribute :description, :string
  attribute :tag_list, :string

  #Atrributes for PorductSize
  attribute :category_id, :string
  attribute :size, :string
  attribute :quantity, :string

#   def persisted?
#     false
#   end

#   def save
#     if valid?
#       persist!
#       true
#     else
#       false
#     end
#   end

# private

#   def persist!
#     @product = Product.create!()
#     @product_size = ProductSize.create!()
#   end
end

Aucun commentaire:

Enregistrer un commentaire