custom form inside a repeat, passing a value from the repeat context into the form

15 views
Skip to first unread message

hobo_hippy

unread,
Jan 10, 2012, 9:31:09 PM1/10/12
to Hobo Users
Ok. I'm getting the custom form to do almost everything I need it to.
Only problem is I don't want the user to have to specify what item
they're adding (a belongs to association of the model). I'm writing
the form in a repeat block where the local context is the item I want
add. Something like

<repeat with="&@foods">
<%= this.name %>
<%= this.price %>

<form with="&OrderAssist.create">
<field-list fields="toppings, food"/>
<submit label="add"/>
</form>

</repeat>

so i figured there must be some way to pass the current food id to the
OrderAssist. However, I feel like the local context would get jumbled
because inside the form "this" would refer to the OrderAssist, not the
food.

How do I pass the current food from the repeat to the OrderAssist i'm
creating rather than making a user choose a food from a select button?

hobo_hippy

unread,
Jan 10, 2012, 9:32:22 PM1/10/12
to Hobo Users
To be clear, i don't want to list the "food" field, I want the form to
use the current food from the repeat. How can I do this?

Matt Jones

unread,
Jan 10, 2012, 10:01:54 PM1/10/12
to hobo...@googlegroups.com

If you've got a belongs_to relation between OrderAssist and Food, thus:

class Food
has_many :order_assists
end

class OrderAssist
belongs_to :food
end

then you can do this:

<repeat with="&@foods">
<form with="&this.order_assists.build">
<% in here, this.food should be set correctly. <form> will (I believe) even create a hidden-field for it if you don't mention it in the field-list %>
</form>
</repeat>

You probably don't want to use OrderAssist.create in any case - that spams unattached records into your DB...

--Matt Jones


hobo_hippy

unread,
Jan 11, 2012, 5:26:11 PM1/11/12
to Hobo Users
that works nicely. The .build function is cool... where can i find
documentation for that? and what do you mean .create spams unattached
records in the database?

-hobohippy

kevinpfromnm

unread,
Jan 12, 2012, 3:51:41 PM1/12/12
to hobo...@googlegroups.com
create makes a new instance and saves it to the database immediately.  by using it in a view, anytime the view is loaded a new record will be created.

hobo_hippy

unread,
Jan 17, 2012, 7:05:09 PM1/17/12
to Hobo Users
oh. that makes sense. thanks i'll avoid that in the future.

one last tweak on the .build example above. I'm trying to figure out
how to change the field-list such that instead of a drop-menu select-
many, i want a list of checkboxes for each of the toppings. Is there
an easy way to do this?

-hoboHippy

hobo_hippy

unread,
Jan 17, 2012, 8:21:33 PM1/17/12
to Hobo Users
this is a new problem, kinda, so i'm gonna start another thread
Reply all
Reply to author
Forward
0 new messages