Okay so with my application users are able to customize a product (via the
flexi variants gem) and then add it to their cart. What I want to do is
save the customizations that are created in a new model (productbuilds)
when the cart form is submitted. I am trying to use the
accepts_nested_attributes_for method in rails and nesting productbuilds
under orders. I thought I had everything set up correctly and I am
receiving no errors, yet nothing is inserted into the productbuilds table.
I think the issue is in how I have it setup in the orders controller, but
I set it up as per any resource I have found. Any thoughts or suggestions?


*Controllers:*
*
*
*orders_conrtoller_decorator.rb*

def populate
@order = current_order(true)
@order.productbuilds.build

params[:products].each do |product_id,variant_id|
quantity = params[:quantity].to_i if !params[:quantity].is_a?(Hash)
quantity = params[:quantity][variant_id].to_i if
params[:quantity].is_a?(Hash)
@order.add_variant(Variant.find(variant_id), quantity,
ad_hoc_option_value_ids, product_customizations) if quantity > 0
end if params[:products]

params[:variants].each do |variant_id, quantity|
quantity = quantity.to_i
@order.add_variant(Variant.find(variant_id), quantity,
ad_hoc_option_value_ids, product_customizations) if quantity > 0
end if params[:variants]

fire_event('spree.cart.add')
fire_event('spree.order.contents_changed')

redirect_to cart_path
end



*Models:*

*order_decorator.rb*
*
*
module Spree
class Order < ActiveRecord::Base
attr_accessible :productbuilds_attributes

has_many :productbuilds

accepts_nested_attributes_for :productbuilds
end
end


*productbuild.rb*
module Spree
class Productbuild < ActiveRecord::Base
attr_accessible :order_id

belongs_to :order
end
end


*Views:*
*
*
*_cart_form.html*
*
*
<%= form_for :order, :url => populate_orders_url do |f| %>
<div id="inside-product-cart-form" data-hook="inside_product_cart_form"
itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<%= f.fields_for :productbuilds do |productbuild_form| %>
<%= productbuild_form.hidden_field :tops_size, :value => "Small"%>
<%= productbuild_form.hidden_field :tops_style, :value => "Tri Top" %>
<% end %>

............
<% end %>

--
You received this message because you are subscribed to the Google Groups "Spree" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spree-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ryan Bigg at Jan 30, 2013 at 4:04 am
    Hey Greg,

    Are you able to put this in a new Rails app and then put that app up on
    GitHub for me? That'd make it easier to inspect the code as a whole and
    track down this problem quickly.

    Thanks!

    On Wed, Jan 30, 2013 at 7:55 AM, Greg Murray wrote:

    Okay so with my application users are able to customize a product (via the
    flexi variants gem) and then add it to their cart. What I want to do is
    save the customizations that are created in a new model (productbuilds)
    when the cart form is submitted. I am trying to use the
    accepts_nested_attributes_for method in rails and nesting productbuilds
    under orders. I thought I had everything set up correctly and I am
    receiving no errors, yet nothing is inserted into the productbuilds table.
    I think the issue is in how I have it setup in the orders controller, but
    I set it up as per any resource I have found. Any thoughts or suggestions?


    *Controllers:*
    *
    *
    *orders_conrtoller_decorator.rb*

    def populate
    @order = current_order(true)
    @order.productbuilds.build

    params[:products].each do |product_id,variant_id|
    quantity = params[:quantity].to_i if !params[:quantity].is_a?(Hash)
    quantity = params[:quantity][variant_id].to_i if
    params[:quantity].is_a?(Hash)
    @order.add_variant(Variant.find(variant_id), quantity,
    ad_hoc_option_value_ids, product_customizations) if quantity > 0
    end if params[:products]

    params[:variants].each do |variant_id, quantity|
    quantity = quantity.to_i
    @order.add_variant(Variant.find(variant_id), quantity,
    ad_hoc_option_value_ids, product_customizations) if quantity > 0
    end if params[:variants]

    fire_event('spree.cart.add')
    fire_event('spree.order.contents_changed')

    redirect_to cart_path
    end



    *Models:*

    *order_decorator.rb*
    *
    *
    module Spree
    class Order < ActiveRecord::Base
    attr_accessible :productbuilds_attributes

    has_many :productbuilds

    accepts_nested_attributes_for :productbuilds
    end
    end


    *productbuild.rb*
    module Spree
    class Productbuild < ActiveRecord::Base
    attr_accessible :order_id

    belongs_to :order
    end
    end


    *Views:*
    *
    *
    *_cart_form.html*
    *
    *
    <%= form_for :order, :url => populate_orders_url do |f| %>
    <div id="inside-product-cart-form" data-hook="inside_product_cart_form"
    itemprop="offers" itemscope itemtype="http://schema.org/Offer">
    <%= f.fields_for :productbuilds do |productbuild_form| %>
    <%= productbuild_form.hidden_field :tops_size, :value => "Small"%>
    <%= productbuild_form.hidden_field :tops_style, :value => "Tri Top" %>
    <% end %>

    ............
    <% end %>

    --
    You received this message because you are subscribed to the Google Groups
    "Spree" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to spree-user+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "Spree" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to spree-user+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ryan Bigg at Feb 3, 2013 at 10:57 pm
    I guess the first line would go in the new action, but why do you need the
    second line? Wouldn't nested attributes take care of that?

    On Sat, Feb 2, 2013 at 6:41 AM, Greg Murray wrote:

    Ok found a fix. Added this to the orders_controller_decorator and it is
    now working.

    @order.build_productbuild
    @order.productbuild = Productbuild.new(params[:order][:productbuild])


    On Wednesday, January 30, 2013 9:48:13 AM UTC-8, Greg Murray wrote:

    Hey Ryan,

    Thanks for your help on this. I have added the files to a new repo at
    https://github.com/**gregmurray/nested_debug<https://github.com/gregmurray/nested_debug>

    To me it seems the problem may be in the orders controller decorator and
    how spree handles creating a new order where @order =
    current_order(true), @order.**productbuilds.build is used instead of
    the typical nested model format that works for me on other simpler apps
    i.e. @order = Order.new, @order.productbuilds.build


    By the way here are the parameters when the form is submitted

    Parameters: {"utf8"=>"✓", "authenticity_token"=>"**
    5YO2InDUw94bsNoHDhjpzOqcofhGJs**S8LI1nIVyuAuY=",
    "order"=>{"productbuilds"=>{"**order_id"=>"1069267047",
    "tops_size"=>"Small", "tops_style"=>"Tri Top", "tops_color"=>""}},
    "variants"=>{"1013589413"=>"1"**}, "ad_hoc_option_values"=>{"15"=**>"",
    "18"=>"", "19"=>"", "20"=>"", "21"=>"", "26"=>"", "17"=>"62", "22"=>"",
    "23"=>"", "24"=>"", "25"=>"", "27"=>""}}


    Thanks again for your help Ryan.


    On Tuesday, January 29, 2013 8:04:49 PM UTC-8, Ryan Bigg wrote:

    Hey Greg,

    Are you able to put this in a new Rails app and then put that app up on
    GitHub for me? That'd make it easier to inspect the code as a whole and
    track down this problem quickly.

    Thanks!

    On Wed, Jan 30, 2013 at 7:55 AM, Greg Murray wrote:

    Okay so with my application users are able to customize a product (via
    the flexi variants gem) and then add it to their cart. What I want to do
    is save the customizations that are created in a new model (productbuilds)
    when the cart form is submitted. I am trying to use the
    accepts_nested_attributes_for method in rails and nesting productbuilds
    under orders. I thought I had everything set up correctly and I am
    receiving no errors, yet nothing is inserted into the productbuilds table.
    I think the issue is in how I have it setup in the orders controller, but
    I set it up as per any resource I have found. Any thoughts or suggestions?


    *Controllers:*
    *
    *
    *orders_conrtoller_decorator.rb*

    def populate
    @order = current_order(true)
    @order.productbuilds.build

    params[:products].each do |product_id,variant_id|
    quantity = params[:quantity].to_i if
    !params[:quantity].is_a?(Hash)
    quantity = params[:quantity][variant_id].**to_i if
    params[:quantity].is_a?(Hash)
    @order.add_variant(Variant.**find(variant_id), quantity,
    ad_hoc_option_value_ids, product_customizations) if quantity > 0
    end if params[:products]

    params[:variants].each do |variant_id, quantity|
    quantity = quantity.to_i
    @order.add_variant(Variant.**find(variant_id), quantity,
    ad_hoc_option_value_ids, product_customizations) if quantity > 0
    end if params[:variants]

    fire_event('spree.cart.add')
    fire_event('spree.order.**contents_changed')

    redirect_to cart_path
    end



    *Models:*

    *order_decorator.rb*
    *
    *
    module Spree
    class Order < ActiveRecord::Base
    attr_accessible :productbuilds_attributes

    has_many :productbuilds

    accepts_nested_attributes_for :productbuilds
    end
    end


    *productbuild.rb*
    module Spree
    class Productbuild < ActiveRecord::Base
    attr_accessible :order_id

    belongs_to :order
    end
    end


    *Views:*
    *
    *
    *_cart_form.html*
    *
    *
    <%= form_for :order, :url => populate_orders_url do |f| %>
    <div id="inside-product-cart-form" data-hook="inside_product_**cart_form"
    itemprop="offers" itemscope itemtype="http://schema.org/**Offer<http://schema.org/Offer>
    ">
    <%= f.fields_for :productbuilds do |productbuild_form| %>
    <%= productbuild_form.hidden_field :tops_size, :value => "Small"%>
    <%= productbuild_form.hidden_field :tops_style, :value => "Tri
    Top" %>
    <% end %>

    ............
    <% end %>

    --
    You received this message because you are subscribed to the Google
    Groups "Spree" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to spree-user+...@googlegroups.**com.
    For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
    .

    --
    You received this message because you are subscribed to the Google Groups
    "Spree" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to spree-user+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "Spree" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to spree-user+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Greg Murray at Feb 9, 2013 at 10:14 pm
    Hey Ryan, just simply adding nested attributes didn't take care of it
    properly for some reason. The only way I could get it to function was to
    add the second line.

    On Sunday, February 3, 2013 2:57:49 PM UTC-8, Ryan Bigg wrote:

    I guess the first line would go in the new action, but why do you need the
    second line? Wouldn't nested attributes take care of that?


    On Sat, Feb 2, 2013 at 6:41 AM, Greg Murray <gr...@me.com <javascript:>>wrote:
    Ok found a fix. Added this to the orders_controller_decorator and it is
    now working.

    @order.build_productbuild
    @order.productbuild = Productbuild.new(params[:order][:productbuild])


    On Wednesday, January 30, 2013 9:48:13 AM UTC-8, Greg Murray wrote:

    Hey Ryan,

    Thanks for your help on this. I have added the files to a new repo at
    https://github.com/**gregmurray/nested_debug<https://github.com/gregmurray/nested_debug>

    To me it seems the problem may be in the orders controller decorator and
    how spree handles creating a new order where @order =
    current_order(true), @order.**productbuilds.build is used instead of
    the typical nested model format that works for me on other simpler apps
    i.e. @order = Order.new, @order.productbuilds.build


    By the way here are the parameters when the form is submitted

    Parameters: {"utf8"=>"✓", "authenticity_token"=>"**
    5YO2InDUw94bsNoHDhjpzOqcofhGJs**S8LI1nIVyuAuY=",
    "order"=>{"productbuilds"=>{"**order_id"=>"1069267047",
    "tops_size"=>"Small", "tops_style"=>"Tri Top", "tops_color"=>""}},
    "variants"=>{"1013589413"=>"1"**}, "ad_hoc_option_values"=>{"15"=**>"",
    "18"=>"", "19"=>"", "20"=>"", "21"=>"", "26"=>"", "17"=>"62", "22"=>"",
    "23"=>"", "24"=>"", "25"=>"", "27"=>""}}


    Thanks again for your help Ryan.


    On Tuesday, January 29, 2013 8:04:49 PM UTC-8, Ryan Bigg wrote:

    Hey Greg,

    Are you able to put this in a new Rails app and then put that app up on
    GitHub for me? That'd make it easier to inspect the code as a whole and
    track down this problem quickly.

    Thanks!

    On Wed, Jan 30, 2013 at 7:55 AM, Greg Murray wrote:

    Okay so with my application users are able to customize a product (via
    the flexi variants gem) and then add it to their cart. What I want to do
    is save the customizations that are created in a new model (productbuilds)
    when the cart form is submitted. I am trying to use the
    accepts_nested_attributes_for method in rails and nesting productbuilds
    under orders. I thought I had everything set up correctly and I am
    receiving no errors, yet nothing is inserted into the productbuilds table.
    I think the issue is in how I have it setup in the orders controller, but
    I set it up as per any resource I have found. Any thoughts or suggestions?


    *Controllers:*
    *
    *
    *orders_conrtoller_decorator.rb*

    def populate
    @order = current_order(true)
    @order.productbuilds.build

    params[:products].each do |product_id,variant_id|
    quantity = params[:quantity].to_i if
    !params[:quantity].is_a?(Hash)
    quantity = params[:quantity][variant_id].**to_i if
    params[:quantity].is_a?(Hash)
    @order.add_variant(Variant.**find(variant_id), quantity,
    ad_hoc_option_value_ids, product_customizations) if quantity > 0
    end if params[:products]

    params[:variants].each do |variant_id, quantity|
    quantity = quantity.to_i
    @order.add_variant(Variant.**find(variant_id), quantity,
    ad_hoc_option_value_ids, product_customizations) if quantity > 0
    end if params[:variants]

    fire_event('spree.cart.add')
    fire_event('spree.order.**contents_changed')

    redirect_to cart_path
    end



    *Models:*

    *order_decorator.rb*
    *
    *
    module Spree
    class Order < ActiveRecord::Base
    attr_accessible :productbuilds_attributes

    has_many :productbuilds

    accepts_nested_attributes_for :productbuilds
    end
    end


    *productbuild.rb*
    module Spree
    class Productbuild < ActiveRecord::Base
    attr_accessible :order_id

    belongs_to :order
    end
    end


    *Views:*
    *
    *
    *_cart_form.html*
    *
    *
    <%= form_for :order, :url => populate_orders_url do |f| %>
    <div id="inside-product-cart-form" data-hook="inside_product_**cart_form"
    itemprop="offers" itemscope itemtype="http://schema.org/**Offer<http://schema.org/Offer>
    ">
    <%= f.fields_for :productbuilds do |productbuild_form| %>
    <%= productbuild_form.hidden_field :tops_size, :value => "Small"%>
    <%= productbuild_form.hidden_field :tops_style, :value => "Tri
    Top" %>
    <% end %>

    ............
    <% end %>

    --
    You received this message because you are subscribed to the Google
    Groups "Spree" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to spree-user+...@googlegroups.**com.
    For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
    .

    --
    You received this message because you are subscribed to the Google Groups
    "Spree" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to spree-user+...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "Spree" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to spree-user+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupspree-user @
categoriesrubyonrails
postedJan 29, '13 at 8:55p
activeFeb 9, '13 at 10:14p
posts4
users2
websitespreecommerce.com
irc#RubyOnRails

2 users in discussion

Ryan Bigg: 2 posts Greg Murray: 2 posts

People

Translate

site design / logo © 2022 Grokbase