Hello, I''m clearly missing some bit of code, but I can''t figure out what. There are three models: Invoice, Lineitems and items. I want the user to search a list of items, and then add them to the lineitems table. The seach works, an provides a list on this form: <% form_for :lineitems, :url => {:action => ''create'', :id=> @invoice.id} do |f|-%> <table> <% @items.each do |item| %> <tr> <td><%=item.itemname %></td> <%f.hidden_field :item_id, :value=>item.id %> <td><%= f.text_field :quantity %></td> <td><%= f.text_field :price, :value => item.sellprice %></td> <td><%= f.submit ''Add Item'' %> </td> </tr> <% end %> When you click ''Add Item'', it submits for that last item on the list, not the corresponding item. Controller code for create is: @invoice = Invoice.find(params[:id]) @lineitems = @invoice.lineitems.new(params[:lineitems])
On Sep 6, 6:06 am, maximulus <maximu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I''m clearly missing some bit of code, but I can''t figure out what. > There are three models: Invoice, Lineitems and items. I want the user > to search a list of items, and then add them to the lineitems table. > The seach works, an provides a list on this form: > > <% form_for :lineitems, :url => {:action => ''create'', :id=> > @invoice.id} do |f|-%> > <table> > > <% @items.each do |item| %> > > <tr> > <td><%=item.itemname %></td> <%> f.hidden_field :item_id, :value=>item.id %> > <td><%= f.text_field :quantity %></td> > <td><%= f.text_field :price, :value => item.sellprice %></td> > > <td><%= f.submit ''Add Item'' %> </td> > </tr> > <% end %> > > When you click ''Add Item'', it submits for that last item on the list, > not the corresponding item.Well you''ve only got one form, so when you hit submit all of the item_id, quantity and price parameters are submitted and in this case when faced with a repeated parameter rails picks the last one. The browser doesn''t know that because you''ve hit the submit button for one row you only want to submit those fields that are close to it. Fred> Controller code for create is: > @invoice = Invoice.find(params[:id]) > > @lineitems = @invoice.lineitems.new(params[:lineitems])
2009/9/6 maximulus <maximulus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Hello, > > I''m clearly missing some bit of code, but I can''t figure out what. > There are three models: Invoice, Lineitems and items. I want the user > to search a list of items, and then add them to the lineitems table. > The seach works, an provides a list on this form: > > > <% form_for :lineitems, :url => {:action => ''create'', :id=> > @invoice.id} do |f|-%> > <table> > > <% @items.each do |item| %> > > <tr> > <td><%=item.itemname %></td> <%> f.hidden_field :item_id, :value=>item.id %>Probably nothing to do with your problem but I believe that input fields (hidden or not) are not allowed between td elements. To check, view the source of the page in the browser and cut and paste the whole thing into the html validation service at http://validator.w3.org/#validate_by_input. It may work ok as you have it in one browser but not another. I imagine you can put it inside the cell. Colin> <td><%= f.text_field :quantity %></td> > <td><%= f.text_field :price, :value => item.sellprice %></td> > > <td><%= f.submit ''Add Item'' %> </td> > </tr> > <% end %> > > When you click ''Add Item'', it submits for that last item on the list, > not the corresponding item. > > Controller code for create is: > @invoice = Invoice.find(params[:id]) > > @lineitems = @invoice.lineitems.new(params[:lineitems]) > > > >
Hello, Thanks for all the replies. I''ve used this format before in .Net using a datagrid, but I''m tring to apply it to RoR. Fred, if the issue is repeated parameter in a single form, am I best off attempting to add ''fields_for'' to the form, or is there a way to make every row iterate on multiple forms, like placing @item.each do outside the form (I would assume this would not work.) Thanks Colin, once I get a functional page, I''ll validate it. max On Sep 6, 10:06 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/9/6 maximulus <maximu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > > > Hello, > > > I''m clearly missing some bit of code, but I can''t figure out what. > > There are three models: Invoice, Lineitems and items. I want the user > > to search a list of items, and then add them to the lineitems table. > > The seach works, an provides a list on this form: > > > <% form_for :lineitems, :url => {:action => ''create'', :id=> > > @invoice.id} do |f|-%> > > <table> > > > <% @items.each do |item| %> > > > <tr> > > <td><%=item.itemname %></td> <%> > f.hidden_field :item_id, :value=>item.id %> > > Probably nothing to do with your problem but I believe that input > fields (hidden or not) are not allowed between td elements. To check, > view the source of the page in the browser and cut and paste the whole > thing into the html validation service athttp://validator.w3.org/#validate_by_input. It may work ok as you > have it in one browser but not another. I imagine you can put it > inside the cell. > > Colin > > > > > <td><%= f.text_field :quantity %></td> > > <td><%= f.text_field :price, :value => item.sellprice %></td> > > > <td><%= f.submit ''Add Item'' %> </td> > > </tr> > > <% end %> > > > When you click ''Add Item'', it submits for that last item on the list, > > not the corresponding item. > > > Controller code for create is: > > @invoice = Invoice.find(params[:id]) > > > @lineitems = @invoice.lineitems.new(params[:lineitems])- Hide quoted text - > > - Show quoted text -- Hide quoted text - > > - Show quoted text -
On Sun, Sep 6, 2009 at 7:50 AM, maximulus<maximulus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Colin, once I get a functional page, I''ll validate it.Creating valid markup, as well as valid code, is how you *get to* a "functional page" -- it''s not an afterthought. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
2009/9/6 Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > On Sun, Sep 6, 2009 at 7:50 AM, maximulus<maximulus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Thanks Colin, once I get a functional page, I''ll validate it. > > Creating valid markup, as well as valid code, is how you *get to* a > "functional page" -- it''s not an afterthought.Are you saying you do not check that it is valid afterwards? A page can appear to be functional even with invalid markup so I always check mine after it appears to work just in case I have made a mistake. Colin
On Sun, Sep 6, 2009 at 10:18 AM, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:>> Creating valid markup, as well as valid code, is how you *get to* a >> "functional page" -- it''s not an afterthought. > > Are you saying you do not check that it is valid afterwards?"After" what? Validation, just like running unit tests, needs to be done throughout development, as part of each iteration/refactoring. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
Again, thanks for the replies. I never said I don''t do it. But my issue doesn''t work with any browser at the moment, and even if I eliminate all the td elements (or the entire table), the same problem persists. The problem is in the ruby code, not the markup html yet, which is why I elect to do it after I have the ruby working. if I take out the html for the table and submit the code for the view like this: <% form_for :lineitems, :url => {:action => ''create'', :id=> @invoice.id} do |f|-%> <% @items.each do |item| %> <%=item.itemname %> <%= f.hidden_field :item_id, :value=>item.id %> <%= f.text_field :quantitydelivered %> <%= f.text_field :price, :value => item.sellprice %> <%= f.submit ''Add Item'' %> <% end %> <% end %> I still get the same issue as the original inquiry. As for how to get a button to submit a line data, I''m still waiting for anyone who has an idea, direction of where I''ve gone wrong or even source code I could take a look at. On Sun, Sep 6, 2009 at 1:18 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 2009/9/6 Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > On Sun, Sep 6, 2009 at 7:50 AM, maximulus<maximulus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >> Thanks Colin, once I get a functional page, I''ll validate it. > > > > Creating valid markup, as well as valid code, is how you *get to* a > > "functional page" -- it''s not an afterthought. > > Are you saying you do not check that it is valid afterwards? A page > can appear to be functional even with invalid markup so I always check > mine after it appears to work just in case I have made a mistake. > > Colin > > > >--~--~---------~--~----~------------~-------~--~----~ 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/6 Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > On Sun, Sep 6, 2009 at 10:18 AM, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >>> Creating valid markup, as well as valid code, is how you *get to* a >>> "functional page" -- it''s not an afterthought. >> >> Are you saying you do not check that it is valid afterwards? > > "After" what?After it appears to be functional.> > Validation, just like running unit tests, needs to be done throughout > development, as part of each iteration/refactoring.Obviously, each time it is modified and appears to be functional again then re-validate. There is no point validating it before it is functional (unless not sure why it is not functional of course in which case validating it is useful). Colin> > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > twitter: @hassan > > > >
On Sun, Sep 6, 2009 at 10:48 AM, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> ... There is no point validating it before it is > functional (unless not sure why it is not functional of course in > which case validating it is useful).Exactly my point. It is totally possible for invalid markup, particularly involving forms (as well as JavaScript), to *prevent* the page from being "functional". Ignoring a failing validation is just like ignoring a failing unit test. You should fix it *now* and move on. # And apologies to the OP for the thread semi-hijack :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan
Apology accepted, but ultimately didn''t this become an issue of ''my coding process is better than your coding process''? Anyway, playing around with it, I got it to work, so in the hope that months from now, someone will come across the same issue, here is the working answer. It was simple, and for some reason I thought I had tried it already and clearly hadn''t, but you actually do seperate each iteration into a seperate form. <% @items.each do |item| %> <% form_for :lineitems, :url => {:action => ''create'', :id=> @invoice.id} do |f|-%> <%=item.itemname %> <%= f.hidden_field :item_id, :value=>item.id %> <%= f.text_field :quantity %> <%= f.text_field :price, :value => item.sellprice %> <%= f.submit ''Add Item'' %> <br/> <% end %> <% end %> Does what I need it to do, though if this can be done as one form, I''d be interested to see the code :-) On Sep 6, 1:58 pm, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Sep 6, 2009 at 10:48 AM, Colin Law<clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > ... There is no point validating it before it is > > functional (unless not sure why it is not functional of course in > > which case validating it is useful). > > Exactly my point. It is totally possible for invalid markup, particularly > involving forms (as well as JavaScript), to *prevent* the page from > being "functional". > > Ignoring a failing validation is just like ignoring a failing unit test. You > should fix it *now* and move on. > > # And apologies to the OP for the thread semi-hijack :-) > > -- > Hassan Schroeder ------------------------ hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > twitter: @hassan
2009/9/6 Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > On Sun, Sep 6, 2009 at 10:48 AM, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> ... There is no point validating it before it is >> functional (unless not sure why it is not functional of course in >> which case validating it is useful). > > Exactly my point. It is totally possible for invalid markup, particularly > involving forms (as well as JavaScript), to *prevent* the page from > being "functional". > > Ignoring a failing validation is just like ignoring a failing unit test. You > should fix it *now* and move on.I never suggested anything else. I don''t know why we are having this discussion really, I pretty much agree with everything you have said. Colin> > # And apologies to the OP for the thread semi-hijack :-) > > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > twitter: @hassan > > > >
I guess you should not use f.submit for "Add item", instead you should use a link and a corresponding action which will add an item and render lineitem form again. Thanks, Abhinav -- अभिनव http://twitter.com/abhinav On Sun, Sep 6, 2009 at 10:36 AM, maximulus <maximulus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello, > > I''m clearly missing some bit of code, but I can''t figure out what. > There are three models: Invoice, Lineitems and items. I want the user > to search a list of items, and then add them to the lineitems table. > The seach works, an provides a list on this form: > > > <% form_for :lineitems, :url => {:action => ''create'', :id=> > @invoice.id} do |f|-%> > <table> > > <% @items.each do |item| %> > > <tr> > <td><%=item.itemname %></td> <%> f.hidden_field :item_id, :value=>item.id %> > <td><%= f.text_field :quantity %></td> > <td><%= f.text_field :price, :value => item.sellprice %></td> > > <td><%= f.submit ''Add Item'' %> </td> > </tr> > <% end %> > > When you click ''Add Item'', it submits for that last item on the list, > not the corresponding item. > > Controller code for create is: > @invoice = Invoice.find(params[:id]) > > @lineitems = @invoice.lineitems.new(params[:lineitems]) > > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Your submit button is within the @items.each loop, hence I see that for each row there is an ''Add Item'' button.There should be only 1 submit button in the end (if you want to submit the entire form) Thanks & Regards, Dhruva Sagar. Joan Crawford<http://www.brainyquote.com/quotes/authors/j/joan_crawford.html> - "I, Joan Crawford, I believe in the dollar. Everything I earn, I spend." On Mon, Sep 7, 2009 at 12:29 PM, Abhinav Saxena <abhinav061-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> I guess you should not use f.submit for "Add item", instead you should use > a link and a corresponding action which will add an item and render lineitem > form again. > > Thanks, > Abhinav > -- > अभिनव > http://twitter.com/abhinav > > > > On Sun, Sep 6, 2009 at 10:36 AM, maximulus <maximulus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> >> Hello, >> >> I''m clearly missing some bit of code, but I can''t figure out what. >> There are three models: Invoice, Lineitems and items. I want the user >> to search a list of items, and then add them to the lineitems table. >> The seach works, an provides a list on this form: >> >> >> <% form_for :lineitems, :url => {:action => ''create'', :id=> >> @invoice.id} do |f|-%> >> <table> >> >> <% @items.each do |item| %> >> >> <tr> >> <td><%=item.itemname %></td> <%>> f.hidden_field :item_id, :value=>item.id %> >> <td><%= f.text_field :quantity %></td> >> <td><%= f.text_field :price, :value => item.sellprice %></td> >> >> <td><%= f.submit ''Add Item'' %> </td> >> </tr> >> <% end %> >> >> When you click ''Add Item'', it submits for that last item on the list, >> not the corresponding item. >> >> Controller code for create is: >> @invoice = Invoice.find(params[:id]) >> >> @lineitems = @invoice.lineitems.new(params[:lineitems]) >> >> >> > > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
maximulus wrote:> Apology accepted, but ultimately didn''t this become an issue of ''my > coding process is better than your coding process''?Perhaps it did. And why not? Some coding processes are better than others. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.