BigSmoke
2005-Sep-21 14:31 UTC
Inputting array elements without explicit indices (PHP style)
In PHP I used to be able to do the following: <fieldset><legend>Friends</legend> <input name="friends[]" value="Someone" /> <input name="friends[]" value="Someone a little less nice" /> <input name="friends[]" value="A great person" /> </fieldset> Then, $_REQUEST[''friends''] would contain the following: array(''Someone'', ''Someone a little less nice'', ''A great person'') This notation won''t work in Rails. Is there a way to do something similar in Rails? I''m asking because I''m adding new fields using AJAX calls. Using each_with_index won''t work because I''m adding fields one at a time. Using records'' IDs for the indices doesn''t work because the records don''t yet exist before the record is saved. I could consider using Postgres'' nextval() directly in the Ajax action that returns the new control, but all this doesn''t seem mightily nice. Perhaps I should just cop out and make life mightily easier on myself by not allowing this in the ''new'' action, but only during edit, so that I could instantaneously create and link each new record after each AJAX call. If I haven''t stumbled too much over my own words to be comprehensible, I was hoping that someone could share a solution to a similar problem. - Rowan -- Morality is usually taught by the immoral.
François Beausoleil
2005-Sep-21 14:37 UTC
Re: Inputting array elements without explicit indices (PHP style)
Hi ! BigSmoke said the following on 2005-09-21 10:31:> Perhaps I should just cop out and make life mightily easier on myself > by not allowing this in the ''new'' action, but only during edit, so > that I could instantaneously create and link each new record after > each AJAX call.I know exactly how you feel... The way I handle it at the moment is as soon as the user adds a new entry (phone numbers in my case), I do a full request/response cycle, POSTing the complete form, and redirecting to edit. Then, the second and subsequent adds can use AJAX. I would much prefer finding another solution, though. Thanks ! François
Matthew McKnight
2005-Sep-21 14:50 UTC
Re: Inputting array elements without explicit indices (PHP style)
Pre-AJAX, and in Java, my approach was to use JavaScript (with a ''+'' button) to create new input fields on the form based on the highest existing index. I never submitted the form until someone clicked save. <input name="friends[0]" /> <input name="firends[1]" /> etc. What is the recommend AJAX best practice for this? >BigSmoke said the following on 2005-09-21 10:31:>> Perhaps I should just cop out and make life mightily easier on myself >> by not allowing this in the ''new'' action, but only during edit, so >> that I could instantaneously create and link each new record after >> each AJAX call.On 9/21/05, François Beausoleil <fbeausoleil-IQIa899fVSs@public.gmane.org> wrote:>I know exactly how you feel... The way I handle it at the moment is as >soon as the user adds a new entry (phone numbers in my case), I do a >full request/response cycle, POSTing the complete form, and redirecting >to edit. Then, the second and subsequent adds can use AJAX.-- -Matt McKnight- "let''s push things forward" _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Brian Hogan
2005-Sep-21 15:01 UTC
Re: Inputting array elements without explicit indices (PHP style)
I did something very similar in my app. I needed this form to insert multiple records, so I did the following <% x = 0%> <% for task in @projects.tasks %> <input type="text" value="" name="tasks[<%=x%>][name] /> <input type="text" value="" name="tasks[<%=x%>][description] /> <% x++ %> <% end %> This gives you tasks[0][''task name'', ''a lengthy description of a task''] tasks[1][''another task'', ''yet another description of the task''] .. and so on. Then in the controller, I just collected the items for params[''tasks''] flash[:notice] = "" tasks = params[''tasks''] for item in @tasks task = item[1] # multidimensional array,,, [0] is the key, [1] is the array of value t = Task.new{task[''name''], task[''description''] if t.sa <http://t.sa> flash[:notice] += bt.name <http://bt.name> + '' was updated successfully<br />'' else flash[:notice] += "''" + bt.name <http://bt.name> + "'' was not updated : " + t.errors.full_messages().to_s + ''<br />'' end end I suppose if you''re adding new rows with Ajax, just send the counter param through the URL to your ajax method and increment that in the ajax. Have the ajax send the new value back in a hidden field? I hope somebody finds this useful :) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Gianni Jacklone
2005-Sep-21 15:34 UTC
Re: Inputting array elements without explicit indices (PHP style)
BigSmoke wrote:>In PHP I used to be able to do the following: > ><fieldset><legend>Friends</legend> > <input name="friends[]" value="Someone" /> > <input name="friends[]" value="Someone a little less nice" /> > <input name="friends[]" value="A great person" /> ></fieldset> > >Then, $_REQUEST[''friends''] would contain the following: > >array(''Someone'', ''Someone a little less nice'', ''A great person'') > >This notation won''t work in Rails. Is there a way to do something >similar in Rails? I''m asking because I''m adding new fields using AJAX >calls. Using each_with_index won''t work because I''m adding fields one >at a time. Using records'' IDs for the indices doesn''t work because the >records don''t yet exist before the record is saved. I could consider >using Postgres'' nextval() directly in the Ajax action that returns the >new control, but all this doesn''t seem mightily nice. > > >This might seem a little wacky, but I''ve been using Time.now.to_s as a unique index in my ajax actions for this particular type of situation. It works quite well for me. - Gianni
Trevor Squires
2005-Sep-21 16:11 UTC
Re: Inputting array elements without explicit indices (PHP style)
Hi Brian, comments below: On 21-Sep-05, at 8:01 AM, Brian Hogan wrote:> I did something very similar in my app. I needed this form to insert > multiple records, so I did the following > > <% x = 0%> > <% for task in @projects.tasks %> > <input type="text" value="" name="tasks[<%=x%>][name] /> > <input type="text" value="" name="tasks[<%=x%>][description] /> > <% x++ %> > <% end %> > > > This gives you > tasks[0][''task name'', ''a lengthy description of a task''] > tasks[1][''another task'', ''yet another description of the task''] > .. and so on. >I''m not trying to be pedantic but it doesn''t give you this. It should give you: tasks { ''0'' => {''name'' => ''task name'', ''description'' => ''lengthy description'' }, ''1'' => {''name'' => ''another task'', ''description'' => ''yet another''} } As for suggestions to solve the original poster''s problem, of course it all depends on what you accept and return in your AJAX calls (or to put it another way, how much of the page is updated in the request). My preferred method for this sort of thing is to use array indices (as above) and just use the word ''new'' to signify a new record. I.e. tasks[new][name] My controllers know to look for @params[''tasks''][''new''] as well as @params[''tasks''][/^[0-9]+/] and act accordingly. HTH, Trevor
Brian Hogan
2005-Sep-21 16:21 UTC
Re: Inputting array elements without explicit indices (PHP style)
Trevor: Thanks for the correction. That''s what I get for trying to do too much thinking in the morning. :)> I''m not trying to be pedantic but it doesn''t give you this. It should > give you: > > tasks { > ''0'' => {''name'' => ''task name'', ''description'' => ''lengthy description'' > }, > ''1'' => {''name'' => ''another task'', ''description'' => ''yet another''} > } > > As for suggestions to solve the original poster''s problem, of course it > all depends on what you accept and return in your AJAX calls (or to put > it another way, how much of the page is updated in the request). > > My preferred method for this sort of thing is to use array indices (as > above) and just use the word ''new'' to signify a new record. > > I.e. tasks[new][name] > > My controllers know to look for @params[''tasks''][''new''] as well as > @params[''tasks''][/^[0-9]+/] and act accordingly. > > HTH, > Trevor > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ezra Zygmuntowicz
2005-Sep-21 22:39 UTC
Re: Inputting array elements without explicit indices (PHP style)
On Sep 21, 2005, at 9:11 AM, Trevor Squires wrote:> Hi Brian, > > comments below: > > On 21-Sep-05, at 8:01 AM, Brian Hogan wrote: > > >> I did something very similar in my app. I needed this form to insert >> multiple records, so I did the following >> >> <% x = 0%> >> <% for task in @projects.tasks %> >> <input type="text" value="" name="tasks[<%=x%>][name] /> >> <input type="text" value="" name="tasks[<%=x%>][description] /> >> <% x++ %> >> <% end %> >>I don''t think x++ works in ruby you need x+=1>> >> This gives you >> tasks[0][''task name'', ''a lengthy description of a task''] >> tasks[1][''another task'', ''yet another description of the task''] >> .. and so on. >> >> > > I''m not trying to be pedantic but it doesn''t give you this. It > should give you: > > tasks { > ''0'' => {''name'' => ''task name'', ''description'' => ''lengthy > description'' }, > ''1'' => {''name'' => ''another task'', ''description'' => ''yet another''} > } > > As for suggestions to solve the original poster''s problem, of > course it all depends on what you accept and return in your AJAX > calls (or to put it another way, how much of the page is updated in > the request). > > My preferred method for this sort of thing is to use array indices > (as above) and just use the word ''new'' to signify a new record. > > I.e. tasks[new][name] > > My controllers know to look for @params[''tasks''][''new''] as well as > @params[''tasks''][/^[0-9]+/] and act accordingly. > > HTH, > Trevor > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
BigSmoke
2005-Sep-24 13:59 UTC
Re: Inputting array elements without explicit indices (PHP style)
On 9/21/05, Gianni Jacklone <gianni@one6ix.com> wrote:> BigSmoke wrote: > > >In PHP I used to be able to do the following: > > > ><fieldset><legend>Friends</legend> > > <input name="friends[]" value="Someone" /> > > <input name="friends[]" value="Someone a little less nice" /> > > <input name="friends[]" value="A great person" /> > ></fieldset> > > > >Then, $_REQUEST['friends'] would contain the following: > > > >array('Someone', 'Someone a little less nice', 'A great person') > > > >This notation won't work in Rails. Is there a way to do something > >similar in Rails? I'm asking because I'm adding new fields using AJAX > >calls. Using each_with_index won't work because I'm adding fields one > >at a time. Using records' IDs for the indices doesn't work because the > >records don't yet exist before the record is saved. I could consider > >using Postgres' nextval() directly in the Ajax action that returns the > >new control, but all this doesn't seem mightily nice. > > > This might seem a little wacky, but I've been using Time.now.to_s as a > unique index in my ajax actions for this particular type of situation. > It works quite well for me. >Wacky problems call for wacky solutions. I had actually been thinking about using a random index, but, when using a random number as index, I was afraid it might occur twice, and adding a check to ensure unicity seemed a bit too much for such a simple problem. Time.now does seem to solve the duplicity problem. Compared to my own wacky idea, at least, your kludge does seem to have a certain robustness. - Rowan -- Morality is usually taught by the immoral. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
BigSmoke
2005-Sep-24 14:22 UTC
Re: Inputting array elements without explicit indices (PHP style)
On 9/21/05, BigSmoke <bigsmoke@gmail.com> wrote:> In PHP I used to be able to do the following: > > <fieldset><legend>Friends</legend> > <input name="friends[]" value="Someone" /> > <input name="friends[]" value="Someone a little less nice" /> > <input name="friends[]" value="A great person" /> > </fieldset> > > Then, $_REQUEST['friends'] would contain the following: > > array('Someone', 'Someone a little less nice', 'A great person') > > This notation won't work in Rails. Is there a way to do something > similar in Rails? I'm asking because I'm adding new fields using AJAX > calls. Using each_with_index won't work because I'm adding fields one > at a time. Using records' IDs for the indices doesn't work because the > records don't yet exist before the record is saved. I could consider > using Postgres' nextval() directly in the Ajax action that returns the > new control, but all this doesn't seem mightily nice. > > Perhaps I should just cop out and make life mightily easier on myself > by not allowing this in the 'new' action, but only during edit, so > that I could instantaneously create and link each new record after > each AJAX call. > > If I haven't stumbled too much over my own words to be comprehensible, > I was hoping that someone could share a solution to a similar problem. > > - RowanI'm wondering what would be a good title for a Wiki page on this subject ... I'd like to summarize some of the solutions offered in this thread. What spring to mind is a page called "HowToInputArrays" with some general information about the array notation and some more specific information about the issues discussed in this thread (e.g.: differences with the PHP notation). Or would I better call this something along the lines of "UnderstandingArrayInput"? - Rowan -- Morality is usually taught by the immoral. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
BigSmoke
2005-Sep-24 14:24 UTC
Re: Inputting array elements without explicit indices (PHP style)
Your method seems quite practical to me. How do you keep track of the index counter? Do you traverse input elements untill you find the highest index each time or do you initialize a counter variable upon the loading of the page? - Rowan On 9/21/05, Matthew McKnight <matt.mcknight@gmail.com> wrote:> Pre-AJAX, and in Java, my approach was to use JavaScript (with a '+' button) > to create new input fields on the form based on the highest existing index. > I never submitted the form until someone clicked save. > <input name="friends[0]" /> > <input name="firends[1]" /> > etc. > > What is the recommend AJAX best practice for this? > > >BigSmoke said the following on 2005-09-21 10:31: > >> Perhaps I should just cop out and make life mightily easier on myself > >> by not allowing this in the 'new' action, but only during edit, so > >> that I could instantaneously create and link each new record after > >> each AJAX call. > > On 9/21/05, François Beausoleil <fbeausoleil@ftml.net> wrote: > >I know exactly how you feel... The way I handle it at the moment is as > >soon as the user adds a new entry (phone numbers in my case), I do a > >full request/response cycle, POSTing the complete form, and redirecting > >to edit. Then, the second and subsequent adds can use AJAX. > > -- > -Matt McKnight- > "let's push things forward"-- Morality is usually taught by the immoral. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Julian ''Julik'' Tarkhanov
2005-Sep-27 10:41 UTC
Re: Inputting array elements without explicit indices (PHP style)
Actually this one looks like a potential patch for cgi_methods.rb (where such requests are handled). Will try to incorporate it in the meantime. On 24-sep-2005, at 15:59, BigSmoke wrote:> On 9/21/05, Gianni Jacklone <gianni-62/Zx0d2zyfQT0dZR+AlfA@public.gmane.org> wrote: > >> BigSmoke wrote: >> >> >>> In PHP I used to be able to do the following: >>> >>> <fieldset><legend>Friends</legend> >>> <input name="friends[]" value="Someone" /> >>> <input name="friends[]" value="Someone a little less nice" /> >>> <input name="friends[]" value="A great person" /> >>> </fieldset> >>> >>> Then, $_REQUEST[''friends''] would contain the following: >>> >>> array(''Someone'', ''Someone a little less nice'', ''A great person'')-- Julian "Julik" Tarkhanov
Kyle Maxwell
2005-Sep-27 11:26 UTC
Re: Inputting array elements without explicit indices (PHP style)
Wouldn''t it be better to create a before_filter in the application controller to handle the php-style post, rather than hack the form fields...? Kyle Maxwell On 9/27/05, Julian ''Julik'' Tarkhanov <listbox-RY+snkucC20@public.gmane.org> wrote:> Actually this one looks like a potential patch for cgi_methods.rb > (where such requests are handled). Will try to incorporate it in the > meantime. > > On 24-sep-2005, at 15:59, BigSmoke wrote: > > > On 9/21/05, Gianni Jacklone <gianni-62/Zx0d2zyfQT0dZR+AlfA@public.gmane.org> wrote: > > > >> BigSmoke wrote: > >> > >> > >>> In PHP I used to be able to do the following: > >>> > >>> <fieldset><legend>Friends</legend> > >>> <input name="friends[]" value="Someone" /> > >>> <input name="friends[]" value="Someone a little less nice" /> > >>> <input name="friends[]" value="A great person" /> > >>> </fieldset> > >>> > >>> Then, $_REQUEST[''friends''] would contain the following: > >>> > >>> array(''Someone'', ''Someone a little less nice'', ''A great person'') > > -- > Julian "Julik" Tarkhanov > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Julian ''Julik'' Tarkhanov
2005-Sep-27 14:10 UTC
Re: Inputting array elements without explicit indices (PHP style)
On 27-sep-2005, at 13:26, Kyle Maxwell wrote:> Wouldn''t it be better to create a before_filter in the application > controller to handle the php-style post, rather than hack the form > fields...? > > Kyle Maxwell > > On 9/27/05, Julian ''Julik'' Tarkhanov <listbox-RY+snkucC20@public.gmane.org> wrote: > >> Actually this one looks like a potential patch for cgi_methods.rb >> (where such requests are handled). Will try to incorporate it in the >> meantime.I don''t quite see it as "hacking" - ActionPack handles CGI requests in a certain fashion, if the stuff added passes the existing tests I don''t think it can''t be added (to Rails trunk). I already had to patch CGI methods a little once and it was not very difficult. -- Julian "Julik" Tarkhanov
BigSmoke
2005-Sep-27 18:26 UTC
Re: Inputting array elements without explicit indices (PHP style)
Comments below ... On 9/21/05, Brian Hogan <bphogan@gmail.com> wrote:> I did something very similar in my app. I needed this form to insert > multiple records, so I did the following > > <% x = 0%> > <% for task in @projects.tasks %> > <input type="text" value="" name="tasks[<%=x%>][name] /> > <input type="text" value="" > name="tasks[<%=x%>][description] /> > <% x++ %> > <% end %>A tip: you could reduce this code to the following: <% @projects.tasks.each_with_index do |x, task| %> <%= text_field_tag "tasks[#{x}][name]", nil %> <%= text_field_tag "tasks[#{x}][description]", nil %> <% end %> I wonder: is this good style or would it be better to write it as one chunk of <% ruby code %>?> This gives you > tasks[0]['task name', 'a lengthy description of a task'] > tasks[1]['another task', 'yet another description of the task'] > .. and so on. > > Then in the controller, I just collected the items for params['tasks'] > > flash[:notice] = "" > tasks = params['tasks'] > for item in @tasks > task = item[1] # multidimensional array,,, [0] is the key, [1] is > the array of value > t = Task.new{task['name'], task['description'] > > if t.sa > flash[:notice] += bt.name + ' was updated successfully<br />' > else > flash[:notice] += "'" + bt.name + "' was not updated : " + > t.errors.full_messages().to_s + '<br />' > end > > end > > I suppose if you're adding new rows with Ajax, just send the counter param > through the URL to your ajax method and increment that in the ajax. Have the > ajax send the new value back in a hidden field? > > I hope somebody finds this useful :)Useful indeed. Thanks. - Rowan -- Morality is usually taught by the immoral. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
BigSmoke
2005-Sep-27 18:34 UTC
Re: Inputting array elements without explicit indices (PHP style)
On 9/27/05, BigSmoke <bigsmoke@gmail.com> wrote:> On 9/21/05, Brian Hogan <bphogan@gmail.com> wrote: > > I did something very similar in my app. I needed this form to insert > > multiple records, so I did the following > > > > <% x = 0%> > > <% for task in @projects.tasks %> > > <input type="text" value="" name="tasks[<%=x%>][name] /> > > <input type="text" value="" > > name="tasks[<%=x%>][description] /> > > <% x++ %> > > <% end %> > > A tip: you could reduce this code to the following: > > <% @projects.tasks.each_with_index do |x, task| %> > <%= text_field_tag "tasks[#{x}][name]", nil %> > <%= text_field_tag "tasks[#{x}][description]", nil %> > <% end %>I meant: <% @projects.tasks.each_with_index do |x, task| %> <%= text_field_tag "tasks[#{x}][name]", task.name %> <%= text_field_tag "tasks[#{x}][description]", task.description %> <% end %> If I weren't using :task, I should have used :each_index - Rowan -- Morality is usually taught by the immoral. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails