Hello,
I am having a problem creating multiple objects of the same model class
from the same form.
For this form, I have a model class called "EventDates". I would like
to
have the user fill in two separate event dates on this form. Here is
what I placed in my template:
<% for @event_date in @event_dates %>
<%= date_select "event_date[]", "event_date", :order
=> [:month, :day, :year],
:include_blank => true %><br />
<%= text_field "event_date[]", "event_time" %>
<br />
<% end %>
This does not work correctly. Even though I submit two event dates,
@params only has the first one. I''d like to go through each event date
and create a new object with the data.
Is there any way of doing this (besides creating two separate EventDate
objects and copying the form above twice?)
Thank you.
Brian
Rather than doing "text_field event_date[]", you should do
"text_field
event_date :index => some_index_variable". Rails will then turn
params[:event_date] into a collection for you automatically.
I usually just break these things out into a partial (e.g.
_event_date.rhtml), and call it with render(:partial =>
''event_date'',
:collection => @event_dates). That creates a local variable for you
called "event_date_counter", which you can then use to set your
":index
=>" values correctly.
Alternately, when it''s a really short snippet, you can just deal with
keeping track of an index yourself.
e.g. (presuming your ActiveRecord model has a field called
"event_date":
<% @yourmodel.each_with_index |the_model, event_index| -%>
<%= text_field "the_model", "event_date", {:index
=> event_index} %>
<% end -%>
--Wilson.
Brian Olsen wrote:> Hello,
>
> I am having a problem creating multiple objects of the same model class
> from the same form.
>
> For this form, I have a model class called "EventDates". I would
like to
> have the user fill in two separate event dates on this form. Here is
> what I placed in my template:
>
> <% for @event_date in @event_dates %>
> <%= date_select "event_date[]", "event_date",
:order => [:month, :day, :year],
> :include_blank => true %><br />
> <%= text_field "event_date[]", "event_time"
%>
> <br />
> <% end %>
>
> This does not work correctly. Even though I submit two event dates,
> @params only has the first one. I''d like to go through each event
date
> and create a new object with the data.
>
> Is there any way of doing this (besides creating two separate EventDate
> objects and copying the form above twice?)
Brian Olsen (Mailing Lists)
2005-Aug-12 04:10 UTC
Re: Creating Two Objects From The Same Form
On Thu, 2005-08-11 at 23:31 -0400, Wilson wrote:> Rather than doing "text_field event_date[]", you should do "text_field > event_date :index => some_index_variable". Rails will then turn > params[:event_date] into a collection for you automatically. > > I usually just break these things out into a partial (e.g. > _event_date.rhtml), and call it with render(:partial => ''event_date'', > :collection => @event_dates). That creates a local variable for you > called "event_date_counter", which you can then use to set your ":index > =>" values correctly. > > Alternately, when it''s a really short snippet, you can just deal with > keeping track of an index yourself. > e.g. (presuming your ActiveRecord model has a field called "event_date": > > <% @yourmodel.each_with_index |the_model, event_index| -%> > <%= text_field "the_model", "event_date", {:index => event_index} %> > <% end -%> > > --Wilson.Thank you for the help. This will help me solve another unrelated problem, as well ... Unfortunately, the :index option does not work with date_select, which is what I use for event_date (works for the other model field, that is with the form, which is text_field.) But a little googling, and I found someone had the same issue, with a patch to boot: http://dev.rubyonrails.com/ticket/847. I do hope this works alright, since it was for version 0.10 ... Brian
That''s too bad.. I was going to suggest this syntax (similar to the
select tag):
<%= date_select ''your_model'',
''event_date'', {:order => [:month, :day,
:year], :include_blank => true}, {:index => event_date_counter} %>
..but from looking at the source, you''re right about needing that
patch.
The patch looks like it should still work fine. Hopefully it will make
it into 1.0, because the current behavior is definitely inconsistent. I
think that all the "to_whatever_tag()" calls should allow for
html_options. Heck, even time_zone_select supports it.
Brian Olsen (Mailing Lists) wrote:> On Thu, 2005-08-11 at 23:31 -0400, Wilson wrote:
>
>>Rather than doing "text_field event_date[]", you should do
"text_field
>>event_date :index => some_index_variable". Rails will then turn
>>params[:event_date] into a collection for you automatically.
>>
>>I usually just break these things out into a partial (e.g.
>>_event_date.rhtml), and call it with render(:partial =>
''event_date'',
>>:collection => @event_dates). That creates a local variable for you
>>called "event_date_counter", which you can then use to set
your ":index
>>=>" values correctly.
>>
>>Alternately, when it''s a really short snippet, you can just
deal with
>>keeping track of an index yourself.
>>e.g. (presuming your ActiveRecord model has a field called
"event_date":
>>
>><% @yourmodel.each_with_index |the_model, event_index| -%>
>> <%= text_field "the_model", "event_date",
{:index => event_index} %>
>><% end -%>
>>
>>--Wilson.
>
>
> Thank you for the help. This will help me solve another unrelated
> problem, as well ...
>
> Unfortunately, the :index option does not work with date_select, which
> is what I use for event_date (works for the other model field, that is
> with the form, which is text_field.) But a little googling, and I found
> someone had the same issue, with a patch to boot:
> http://dev.rubyonrails.com/ticket/847. I do hope this works alright,
> since it was for version 0.10 ...
>
>
> Brian
>
Brian Olsen (Mailing Lists)
2005-Aug-12 05:45 UTC
Re: Creating Two Objects From The Same Form
On Fri, 2005-08-12 at 00:19 -0400, Wilson wrote:> That''s too bad.. I was going to suggest this syntax (similar to the > select tag): > <%= date_select ''your_model'', ''event_date'', {:order => [:month, :day, > :year], :include_blank => true}, {:index => event_date_counter} %> > ..but from looking at the source, you''re right about needing that patch. > > The patch looks like it should still work fine. Hopefully it will make > it into 1.0, because the current behavior is definitely inconsistent. I > think that all the "to_whatever_tag()" calls should allow for > html_options. Heck, even time_zone_select supports it.The patch works. As you say, this should be in 1.0; this should be in all the helpers. Thanks again for the tip. The form works now. Brian
would be so cool if rails was abble to deal with multiple objects creation in the 1.0 release .... it''s possible right now but erro handling and textfield hiughlight on validation fails doesn''t work with the :index => someinteger option.... On 8/12/05, Brian Olsen (Mailing Lists) <mailinglists-PsW/4IK4rJfby3iVrkZq2A@public.gmane.org> wrote:> On Fri, 2005-08-12 at 00:19 -0400, Wilson wrote: > > That''s too bad.. I was going to suggest this syntax (similar to the > > select tag): > > <%= date_select ''your_model'', ''event_date'', {:order => [:month, :day, > > :year], :include_blank => true}, {:index => event_date_counter} %> > > ..but from looking at the source, you''re right about needing that patch. > > > > The patch looks like it should still work fine. Hopefully it will make > > it into 1.0, because the current behavior is definitely inconsistent. I > > think that all the "to_whatever_tag()" calls should allow for > > html_options. Heck, even time_zone_select supports it. > > The patch works. As you say, this should be in 1.0; this should be in > all the helpers. > > Thanks again for the tip. The form works now. > > Brian > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
olivier Hericord wrote:>would be so cool if rails was abble to deal with multiple objects >creation in the 1.0 release .... it''s possible right now but erro >handling and textfield hiughlight on validation fails doesn''t work >with the :index => someinteger option.... > >Olivier, it does work... i sent you code for how to do it on a create... so I''m not sure why you say it doesnt work... _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
thanks for the code....my english is not that sharp...:) i meant it''s not possible right out off the box..... rails is so great that sometimes you believe that there is no code to write at all.... LOL thanks for your code ... helps me a lot.... AJAX brought the fact that a helper is needed to create multiple record from one form.... On 8/12/05, Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> wrote:> > olivier Hericord wrote: > > >would be so cool if rails was abble to deal with multiple objects > >creation in the 1.0 release .... it''s possible right now but erro > >handling and textfield hiughlight on validation fails doesn''t work > >with the :index => someinteger option.... > > > > > Olivier, it does work... i sent you code for how to do it on a create... > > so I''m not sure why you say it doesnt work... > > > > _______________________________________________ > 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
Ah... well i thinks its really one of those ''edge cases''... not a lot of demand for it... olivier Hericord wrote:> thanks for the code....my english is not that sharp...:) > i meant it''s not possible right out off the box..... > rails is so great that sometimes you believe that there is no code to > write at all.... LOL > > thanks for your code ... helps me a lot.... > AJAX brought the fact that a helper is needed to create multiple > record from one form.... > > On 8/12/05, *Sean T Allen* < sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org > <mailto:sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org>> wrote: > > olivier Hericord wrote: > > >would be so cool if rails was abble to deal with multiple objects > >creation in the 1.0 release .... it''s possible right now but erro > >handling and textfield hiughlight on validation fails doesn''t work > >with the :index => someinteger option.... > > > > > Olivier, it does work... i sent you code for how to do it on a > create... > > so I''m not sure why you say it doesnt work... > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org <mailto: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 > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
This does work. You just need to make sure the "object" parameter to
your
helpers is an instance variable (e.g. for <%= text_field
''blah'', ''something'' %>,
@blah needs to exist.)
..and if you need multiple "error_messages_for" items on the same
page, you need
to use the ":id" option.
Here''s an example that combines both in a partial form called
"_gizmo.rhtml"
<% @gizmo = gizmo -%>
<%= error_messages_for(''gizmo'', :id =>
"errorExplanation#{gizmo_counter}") %>
--Wilson.
olivier Hericord wrote:> would be so cool if rails was abble to deal with multiple objects
> creation in the 1.0 release .... it''s possible right now but erro
> handling and textfield hiughlight on validation fails doesn''t work
> with the :index => someinteger option....
>
> On 8/12/05, Brian Olsen (Mailing Lists)
<mailinglists-PsW/4IK4rJfby3iVrkZq2A@public.gmane.org> wrote:
>
>>On Fri, 2005-08-12 at 00:19 -0400, Wilson wrote:
>>
>>>That''s too bad.. I was going to suggest this syntax
(similar to the
>>>select tag):
>>><%= date_select ''your_model'',
''event_date'', {:order => [:month, :day,
>>>:year], :include_blank => true}, {:index =>
event_date_counter} %>
>>>..but from looking at the source, you''re right about
needing that patch.
>>>
>>>The patch looks like it should still work fine. Hopefully it will
make
>>>it into 1.0, because the current behavior is definitely
inconsistent. I
>>>think that all the "to_whatever_tag()" calls should allow
for
>>>html_options. Heck, even time_zone_select supports it.
>>
>>The patch works. As you say, this should be in 1.0; this should be in
>>all the helpers.
>>
>>Thanks again for the tip. The form works now.
>>
>>Brian
>>
>>_______________________________________________
>>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
>