On Dec 18, 9:50 pm, Manuca <[email protected]> wrote:
>
> What I found in those tutorials about complex forms is that they
> create relations on the fly to new objects (also created in the same
> process) and this case is slightly different you need to establish a
> relation between the object you are instantiating and an existing one.
>
> In my case I have a model say House and I need to assign it a Category
> instance, but I don't want to instantiate a new Category for that
> object, I just need to associate that object with an existing category
> object via its ID, how can I do that? I've been looking at fields_for
> and nested models but I don't think this is the case to apply that
> approach, let me know if I'm wrong.
>


I answered the cause my own question by tracking down exactly what the
nil object was.  It turned out to be a misuse of dom_id() on my part.

I believe that the way that this problem is handled in Rails is
through the Javascript libraries.  While it is possible to create both
the relation and the target at the same time (as seems canonical given
the online examples and references) it is not necessary and
appropriate safeguards in the controller will prohibit it.

In the view the question is how to reference existing row values in a
lookup table.  What we need consider is what gets passed back in the
params hash.  When dealing with the case where the association is the
critical element then naturally what we need pass back is a valid
foreign key reference. In the case given above that would likely be a
value assigned to  params[:house][:category_id].  So, in the
controller you test that this is != nil on return (i.e in the create
or update controller methods).  If it is nil then a new Category
object is being requested and you need handle that circumstance as
appropriate (Raise an error or create the object as desired).

In the view you can do the lookup on the reference table several ways
to obtain the reference row id.  The standard approach is a
forms_helper select() that is dynamically populated from the reference
table, displays the some meaningful text in the drop down menu, and
returns the selection's associated row id as the value for the
foreign_key reference.  An alternative is text_field-with_auto_complete
().

The main thing to recall is that since we are not initialising a null
record for the reference table in the controller, then we must not use
any elements of a conjectural row from that table in the view code.
In my case I was using such a reference and that was what caused my
problem.

Hope this helps.  It still muddy in my own mind which is why I cannot
make my explanation clearer.

--

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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to