I''m setting up a basic shopping cart type app, and the products we have will have multiple options. Size, color, etc. I''m trying to figure out how I can collect the the options that are selected when a user adds the item to their cart. An OptionSet is a group of options ("Size", "Color", etc), and an ItemOption is the option itself ("12 oz", "red"). Here''s what I have so far: class Item < ActiveRecord::Base has_many :option_sets end class OptionSet < ActiveRecord::Base has_many :item_options belongs_to :item end # Has fields ''name'' and ''value'' class ItemOption < ActiveRecord::Base belongs_to :option_set end When the user chooses an item and its options, it creates an OrderItem object, which just contains the id of the item, the id of the order, the quantity, and options. I can handle everything easily, except for the options. I don''t know how to generate the select tags, and then store the options with the OrderItem. Can anyone help me out? Thanks, Pat
Also, here''s some code that I have set up in my controller and view: def show @item = Item.find(@params[''id'']) @new_item = OrderItem.new @new_item.order_item_options = Hash.new end The Hash is used to specify which option set is being set, as you''ll see in the view: <% @item.option_sets.each do |option_set| %> <%= collection_select(@new_item, @new_item.order_item_options[option_set.id], option_set.item_options, "value", "name") %> <% end %> When I try that though, I get the following error: Showing /item/show.rhtml where line #12 raised private method `sub!'' called for #<OrderItem:0x1533c0c> Line 12 is the <%= collection_select... line. I''m stuck here, looking for some help. On 4/13/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m setting up a basic shopping cart type app, and the products we > have will have multiple options. Size, color, etc. I''m trying to > figure out how I can collect the the options that are selected when a > user adds the item to their cart. An OptionSet is a group of options > ("Size", "Color", etc), and an ItemOption is the option itself ("12 > oz", "red"). Here''s what I have so far: > > class Item < ActiveRecord::Base > has_many :option_sets > end > > class OptionSet < ActiveRecord::Base > has_many :item_options > belongs_to :item > end > > # Has fields ''name'' and ''value'' > class ItemOption < ActiveRecord::Base > belongs_to :option_set > end > > When the user chooses an item and its options, it creates an OrderItem > object, which just contains the id of the item, the id of the order, > the quantity, and options. I can handle everything easily, except for > the options. I don''t know how to generate the select tags, and then > store the options with the OrderItem. Can anyone help me out? > > Thanks, Pat >
I managed to get it going somewhat by not being stupid anymore :) I wasn''t naming the object and methods in quotes...anyway, here''s the code I have up to this point: <% @item.option_sets.each do |option_set| %> <%= collection_select("new_item", "order_item_options", option_set.item_options, "id", "name", { }, "index" => option_set.id) %> <% end %> It seems to work for now...now I have to figure out how to get everything in the db. I''ll ask if I run into any other problems. On 4/13/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Also, here''s some code that I have set up in my controller and view: > > def show > @item = Item.find(@params[''id'']) > @new_item = OrderItem.new > @new_item.order_item_options = Hash.new > end > > The Hash is used to specify which option set is being set, as you''ll > see in the view: > > <% @item.option_sets.each do |option_set| %> > <%= collection_select(@new_item, > @new_item.order_item_options[option_set.id], option_set.item_options, > "value", "name") %> > <% end %> > > When I try that though, I get the following error: > Showing /item/show.rhtml where line #12 raised private method `sub!'' > called for #<OrderItem:0x1533c0c> > Line 12 is the <%= collection_select... line. > > I''m stuck here, looking for some help. > > > On 4/13/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''m setting up a basic shopping cart type app, and the products we > > have will have multiple options. Size, color, etc. I''m trying to > > figure out how I can collect the the options that are selected when a > > user adds the item to their cart. An OptionSet is a group of options > > ("Size", "Color", etc), and an ItemOption is the option itself ("12 > > oz", "red"). Here''s what I have so far: > > > > class Item < ActiveRecord::Base > > has_many :option_sets > > end > > > > class OptionSet < ActiveRecord::Base > > has_many :item_options > > belongs_to :item > > end > > > > # Has fields ''name'' and ''value'' > > class ItemOption < ActiveRecord::Base > > belongs_to :option_set > > end > > > > When the user chooses an item and its options, it creates an OrderItem > > object, which just contains the id of the item, the id of the order, > > the quantity, and options. I can handle everything easily, except for > > the options. I don''t know how to generate the select tags, and then > > store the options with the OrderItem. Can anyone help me out? > > > > Thanks, Pat > > >