Sorry for this basic noob question: I have an "Item" class in my models which corresponds to a SQL table, and a "Cart" class and a "CartItem" class which do not. The Item class does not have an amount object, which is transitory, so I''ve put this amount object in my CartItem class: class CartItem attr_reader :item, :amount def initialize( item ) @item = item end def set_amount( amount ) This.amount = amount end end I have a list of items in a viewer form, and I''d like to pass associated amount to my CartItem when the user hits the "Add to Cart" button. I''ve tried many variations of text_field and text_field_tag in: <% @items.each do |item| %> ... <%= text_field( :amount , :action => ''set_amount( :amount )'', :id => item )%> <%= button_to "Add to Cart" , :action => ''add_to_cart'', :id => item %> ... <% end %> I know the text_field up there is severely mangled and won''t work--but it''s an example of the 2000th try, the last several hundred out of pure desperation. Can anyone help with the proper syntax for the text_field in the .html.emb file? The add_to_cart call with the button is working: def add_to_cart item = Item.find( params[ :id ] ) @cart = find_cart @cart.add_item( item ) end Many TIA, Craig
Text fields don''t take actions, they''re just text fields with some optional aesthetics. put :amount in there, and if item.amount exists, it will be filled in. http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001607 Also, ruby doesn''t use This, it''s "self." There may be other problems in find_cart and add_item. On Sep 13, 12:30 pm, Dudebot <craign...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sorry for this basic noob question: > > I have an "Item" class in my models which corresponds to a SQL table, > and a "Cart" class and a "CartItem" class which do not. The Item > class does not have an amount object, which is transitory, so I''ve put > this amount object in my CartItem class: > > class CartItem > > attr_reader :item, :amount > > def initialize( item ) > @item = item > end > > def set_amount( amount ) > This.amount = amount > end > > end > > I have a list of items in a viewer form, and I''d like to pass > associated amount to my CartItem when the user hits the "Add to Cart" > button. I''ve tried many variations of text_field and text_field_tag > in: > > <% @items.each do |item| %> > ... > <%= text_field( :amount , :action => ''set_amount( :amount )'', :id => > item )%> > <%= button_to "Add to Cart" , :action => ''add_to_cart'', :id => item > %> > ... > <% end %> > > I know the text_field up there is severely mangled and won''t work--but > it''s an example of the 2000th try, the last several hundred out of > pure desperation. Can anyone help with the proper syntax for the > text_field in the .html.emb file? > > The add_to_cart call with the button is working: > > def add_to_cart > item = Item.find( params[ :id ] ) > @cart = find_cart > @cart.add_item( item ) > end > > Many TIA, > Craig
On Sun, Sep 13, 2009 at 12:30 PM, Dudebot <craignied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Sorry for this basic noob question: > > I have an "Item" class in my models which corresponds to a SQL table, > and a "Cart" class and a "CartItem" class which do not. The Item > class does not have an amount object, which is transitory, so I''ve put > this amount object in my CartItem class: > > class CartItem > > attr_reader :item, :amount > > def initialize( item ) > @item = item > end > > def set_amount( amount ) > This.amount = amount > end > > end > > I have a list of items in a viewer form, and I''d like to pass > associated amount to my CartItem when the user hits the "Add to Cart" > button. I''ve tried many variations of text_field and text_field_tag > in: > > <% @items.each do |item| %> > ... > <%= text_field( :amount , :action => ''set_amount( :amount > )'', :id => > item )%> > <%= button_to "Add to Cart" , :action => ''add_to_cart'', :id > => item > %> > ... > <% end %> > > I know the text_field up there is severely mangled and won''t work--but > it''s an example of the 2000th try, the last several hundred out of > pure desperation. Can anyone help with the proper syntax for the > text_field in the .html.emb file? > > The add_to_cart call with the button is working: > > def add_to_cart > item = Item.find( params[ :id ] ) > @cart = find_cart > @cart.add_item( item ) > end > > Many TIA, > Craig >The text_field view helper generates an HTML input tag. Thus, there''s no attribute on the input tag called ''action'' in the X/HTML spec. Next, when to use text_field and text_field_tag? text_field: if you''re creating or updating a model. text_field_tag: if you''re NOT creating or updating a model. Thus, I''m guessing that you''re simply updating a session variable when you''re performing the @cart.add_item( item ) If this is the case, then you should use text_field_tag within a form_tag. You can see example usage of ''form_tag'' and ''text_field_tag'' here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001729 If you are in fact creating or updating a model, then you should use test_field within a form_for. You can see examples usage of ''form_for'' and ''text_field'' here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001607 Finally, I would recommend getting access to a copy of "Agile Web Development with Rails 3rd ed" and/or obtaining the really great screencasts on the subject, "Mastering Rails Form", http://www.pragprog.com/screencasts/v-rbforms/mastering-rails-forms Good luck, -Conrad> > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, Eric. What I''m struggling with is how to set a variable, rather than get one. What would be the proper form for setting SomeObject.amount? Thanks for the correction on "This". Old habits die hard :) On Sep 13, 3:59 pm, Eric <ericgh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Text fields don''t take actions, they''re just text fields with some > optional aesthetics. put :amount in there, and if item.amount exists, > it will be filled in. > > http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html... > > Also, ruby doesn''t use This, it''s "self." > > There may be other problems in find_cart and add_item. > > On Sep 13, 12:30 pm, Dudebot <craign...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Sorry for this basic noob question: > > > I have an "Item" class in my models which corresponds to a SQL table, > > and a "Cart" class and a "CartItem" class which do not. The Item > > class does not have an amount object, which is transitory, so I''ve put > > this amount object in my CartItem class: > > > class CartItem > > > attr_reader :item, :amount > > > def initialize( item ) > > @item = item > > end > > > def set_amount( amount ) > > This.amount = amount > > end > > > end > > > I have a list of items in a viewer form, and I''d like to pass > > associated amount to my CartItem when the user hits the "Add to Cart" > > button. I''ve tried many variations of text_field and text_field_tag > > in: > > > <% @items.each do |item| %> > > ... > > <%= text_field( :amount , :action => ''set_amount( :amount )'', :id => > > item )%> > > <%= button_to "Add to Cart" , :action => ''add_to_cart'', :id => item > > %> > > ... > > <% end %> > > > I know the text_field up there is severely mangled and won''t work--but > > it''s an example of the 2000th try, the last several hundred out of > > pure desperation. Can anyone help with the proper syntax for the > > text_field in the .html.emb file? > > > The add_to_cart call with the button is working: > > > def add_to_cart > > item = Item.find( params[ :id ] ) > > @cart = find_cart > > @cart.add_item( item ) > > end > > > Many TIA, > > Craig
On Sun, Sep 13, 2009 at 2:05 PM, Dudebot <craignied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Thanks, Eric. What I''m struggling with is how to set a variable, > rather than get one. What would be the proper form for setting > SomeObject.amount? > > Thanks for the correction on "This". Old habits die hard :) > > On Sep 13, 3:59 pm, Eric <ericgh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Text fields don''t take actions, they''re just text fields with some > > optional aesthetics. put :amount in there, and if item.amount exists, > > it will be filled in. > > > > http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html... > > > > Also, ruby doesn''t use This, it''s "self." > > > > There may be other problems in find_cart and add_item. > > > > On Sep 13, 12:30 pm, Dudebot <craign...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Sorry for this basic noob question: > > > > > I have an "Item" class in my models which corresponds to a SQL table, > > > and a "Cart" class and a "CartItem" class which do not. The Item > > > class does not have an amount object, which is transitory, so I''ve put > > > this amount object in my CartItem class: > > > > > class CartItem > > > > > attr_reader :item, :amount > > > > > def initialize( item ) > > > @item = item > > > end > > > > > def set_amount( amount ) > > > This.amount = amount > > > end > > > > > end > > > > > I have a list of items in a viewer form, and I''d like to pass > > > associated amount to my CartItem when the user hits the "Add to Cart" > > > button. I''ve tried many variations of text_field and text_field_tag > > > in: > > > > > <% @items.each do |item| %> > > > ... > > > <%= text_field( :amount , :action => ''set_amount( > :amount )'', :id => > > > item )%> > > > <%= button_to "Add to Cart" , :action => ''add_to_cart'', > :id => item > > > %> > > > ... > > > <% end %> > > > > > I know the text_field up there is severely mangled and won''t work--but > > > it''s an example of the 2000th try, the last several hundred out of > > > pure desperation. Can anyone help with the proper syntax for the > > > text_field in the .html.emb file? > > > > > The add_to_cart call with the button is working: > > > > > def add_to_cart > > > item = Item.find( params[ :id ] ) > > > @cart = find_cart > > > @cart.add_item( item ) > > > end > > > > > Many TIA, > > > Craig ><%= text_field( :amount, :value => some_value, :id => item ) %> -Conrad> > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
2009/9/13 Dudebot <craignied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Thanks, Eric. What I''m struggling with is how to set a variable, > rather than get one. What would be the proper form for setting > SomeObject.amount?I don''t understand the question here. Are you trying to set the content of the text field when it is displayed or set a variable in your controller? If the latter then the value the user enters gets passed to the controller in the params hash. Colin
Thanks, Conrad. I did the "depot" tutorial in the Agile Rails book 3rd ed, and the whole pod/screencast on learningrails.com. Both are light on forms. I just downloaded the 3 Mastering Rails Form screencasts, and am going through the first--it''s *excellent*. Just knowing about debug params is worth its weight in gold :) I''ll go through the first one, try my code again, then if I''m still having trouble, repost. Thanks, Colin & Eric for your patience with this noob :) On Sep 13, 4:01 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Sep 13, 2009 at 12:30 PM, Dudebot <craign...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Sorry for this basic noob question: > > > I have an "Item" class in my models which corresponds to a SQL table, > > and a "Cart" class and a "CartItem" class which do not. The Item > > class does not have an amount object, which is transitory, so I''ve put > > this amount object in my CartItem class: > > > class CartItem > > > attr_reader :item, :amount > > > def initialize( item ) > > @item = item > > end > > > def set_amount( amount ) > > This.amount = amount > > end > > > end > > > I have a list of items in a viewer form, and I''d like to pass > > associated amount to my CartItem when the user hits the "Add to Cart" > > button. I''ve tried many variations of text_field and text_field_tag > > in: > > > <% @items.each do |item| %> > > ... > > <%= text_field( :amount , :action => ''set_amount( :amount > > )'', :id => > > item )%> > > <%= button_to "Add to Cart" , :action => ''add_to_cart'', :id > > => item > > %> > > ... > > <% end %> > > > I know the text_field up there is severely mangled and won''t work--but > > it''s an example of the 2000th try, the last several hundred out of > > pure desperation. Can anyone help with the proper syntax for the > > text_field in the .html.emb file? > > > The add_to_cart call with the button is working: > > > def add_to_cart > > item = Item.find( params[ :id ] ) > > @cart = find_cart > > -wb5DWc3F33w@public.gmane.org_item( item ) > > end > > > Many TIA, > > Craig > > The text_field view helper generates an HTML input tag. Thus, there''s no > attribute on the input tag called ''action'' in the X/HTML spec. > Next, when to use text_field and text_field_tag? > > text_field: if you''re creating or updating a model. > text_field_tag: if you''re NOT creating or updating a model. > > Thus, I''m guessing that you''re simply updating a session variable when > you''re performing the > > @cart.add_item( item ) > > If this is the case, then you should use text_field_tag within a form_tag. > You can see > example usage of ''form_tag'' and ''text_field_tag'' here: > > http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.h... > > If you are in fact creating or updating a model, then you should use > test_field within a form_for. > You can see examples usage of ''form_for'' and ''text_field'' here: > > http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html... > > Finally, I would recommend getting access to a copy of "Agile Web > Development with Rails 3rd ed" > and/or obtaining the really great screencasts on the subject, "Mastering > Rails Form", > > http://www.pragprog.com/screencasts/v-rbforms/mastering-rails-forms > > Good luck, > > -Conrad > > > >