On Mar 22, 8:44 pm, Tze Yang Ng <[email protected]> wrote:
> > The kind of situation I'm looking at is where I have an event
> > registration form that includes address information for two different
> > individuals who are part of the registration. Something like this...
>
> > form_for @registration do
>
> >  fields_for @registration.first_person.address do
> >    text_field :street_address
> >  end
>
> >  fields_for @registration.second_person.address do
> >    text_field :street_address
> >  end
>
> > end
>
> > So from this I get two sets of POST parameters 'address
> > [street_address]' , and the second overwrites the first. Obviously not
> > what I'm aiming for. I've tried a few things but haven't found
> > anything yet that works.
>
> Maybe u can try the following:
>
> form_for @registration do
>
>   fields_for :first_person_address do
>     text_field :street_address # , :value =>
> @registration.first_person.address.street_address
>   end
>
>   fields_for :second_person_address do
>     text_field :street_address # , :value =>
> @registration.second_person.address.street_address
>   end
>
> end
>
> The :value is needed if :street_address is editable. U may even need
> to throw in a hidden field for :id, depending on ur requirements.
>
> :] TY
>
> --http://ngty77.blogspot.com


Thanks, I'll try that, although I'm pretty sure that the name
attribute in the html input element will be generated from the model's
class, which would be the same for both :first_person_address
and :second_person_address. So (I think) you'll again end up with the
second input overwriting the first.

However, I have made some other progress on this...

Some more examination of the code and a little experimentation reveals
that the request handler in merb does actually accommodate input's
with names like address[first_person][street_address] and address[]
[street_address]. In the first case, params[address] will be a hash
like {"first_person"=>{"street_address" => "..."}, "second_person" =>
{"street_address" => "..."}. In the second case an array is created
like - [{"street_address" => "..."}, {"street_address" => "..."}].
i.e. just like the way Rails works.

In addition, I found that I can create those inputs by specifying the
name of the field explicitly in the tag helper, i.e. like -

<%= text_field :name => "address[first_person][street_address]" %>

This isn't quite as elegant as Rails, since you have to repeat the
model name in each tag statement instead of relying on fields_for or
form_for to provide it. And afaik you can't use the :index option like
you can in Rails. However this is enough to keep me going for now. I
guess we'll get this extra stuff for free when Rails 3.0 arrives, so
probably not a high priority to add these capabilities to Merb at this
point, but it could be done.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"merb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/merb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to