Init of item in <input-many>

15 views
Skip to first unread message

Bob Sleys

unread,
Mar 8, 2012, 4:05:48 PM3/8/12
to hobo...@googlegroups.com
I need to init a field in that is filled out via an input-many tag.  The thing is I can't init it in the model because the init value comes from a session variable.  I tired using a new_for_model method in the models controller but that doesn't seem to be being called.  I also though about doing it in the dryml but then I ended up over riding the records value in already existing records.

Any ideas?

On a related note I have another field in the same <input-many> tag that I wanted to use :creator => true on the relationship. But when I include the field in the form it isn't defaulting to the current user.

Here is the relationship as defined in the model

  belongs_to :field_tech, :class_name => "User", :creator => true, :conditions => "employee = 't'"

Bob

Bryan Larsen

unread,
Mar 8, 2012, 9:13:51 PM3/8/12
to hobo...@googlegroups.com
On Thu, Mar 8, 2012 at 4:05 PM, Bob Sleys <bsl...@gmail.com> wrote:
> I need to init a field in that is filled out via an input-many tag.  The
> thing is I can't init it in the model because the init value comes from a
> session variable.  I tired using a new_for_model method in the models
> controller but that doesn't seem to be being called.  I also though about
> doing it in the dryml but then I ended up over riding the records value in
> already existing records.

input-many has a 'template' attribute that can be used:

<input-many template="&Foo.new(:bar => 17)"/>

>
> Any ideas?
>
> On a related note I have another field in the same <input-many> tag that I

> wanted to use :creator => true on the relationsahip. But when I include the


> field in the form it isn't defaulting to the current user.
>
> Here is the relationship as defined in the model
>
>   belongs_to :field_tech, :class_name => "User", :creator => true,
> :conditions => "employee = 't'"
>

As far as I'm aware, that little piece of magic only happens for
generated forms, so you'll have to take care of it yourself here.
Here's one way to do it:

<input-many>
<hidden-field:field-tech-id value="&current_user.id"/>
<field-list fields="..."/>
</input-many>

if you don't have hidden-field:
<def tag="hidden-field">
<%= hidden_field_tag(param_name_for_this, this, attributes) %>
</def>

Bryan

Bob Sleys

unread,
Mar 9, 2012, 9:00:42 AM3/9/12
to hobo...@googlegroups.com
Thanks a ton the template worked perfectly.  I end up using it to set both the items the owner and the project from session vars.  Below is the code that worked for me along with some other field tweaks to the input-many for my form.

<input-many template="&Locmathist.new(:field_tech => @current_user, :project => @current_project)">

Bob
Reply all
Reply to author
Forward
0 new messages