# Dynamically generating form tags

**URL:** <https://www.ruby-forum.com/t/dynamically-generating-form-tags/132615>\
**Category:** Rails\
**Created:** [March 21, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/dynamically-generating-form-tags/132615 "2008-03-21T22:04:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 21, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/dynamically-generating-form-tags/132615/1 "2008-03-21T22:04:04Z")

</div>

i’m trying to create a form which has dropdown menus for certain items  
and text boxes for others. i have a feeling i’m trying way too hard to  
make this happen. i have a method which writes strings of the proper  
tags, which are then brought to life by eval() in the view. the strings  
are correct, but something about how eval parses the string only allows  
for one argument, even if i parenthesize them. i feel like i should not  
have to be using eval at all.

any guidance is appreciated.

THE CONTROLLER:

def run  
@script = @@scriptinfo.getTest(params[:choose][:scriptname])  
@parameterforms = @script.parameters.collect { |param|  
chooseForm(param) }  
end

def chooseForm(parameter)  
if parameter.length \<= 1  
“f.text\_field :#{parameter.name}”  
else  
"f.select :#{parameter.name}, #{parameter.values.join(’, ')}] "  
end  
end

THE RUN VIEW:

\<% form\_for :scriptparams, :url =\> {:action =\> “results”} do |f| %\>  
\<% @script.parameters.each\_index do |index| %\>  
\<%= @script.parameters[index].name %\>  
\<%= eval(@parameterforms[index]) %\>  
  
\<% end %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 21, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/dynamically-generating-form-tags/132615/2 "2008-03-21T22:48:02Z")

</div>

would returning a lambda object work?

On Fri, Mar 21, 2008 at 2:04 PM, Reid O.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 22, 2008, 3:02am UTC](https://www.ruby-forum.com/t/dynamically-generating-form-tags/132615/3 "2008-03-22T03:02:06Z")

</div>

Reid O. wrote:

> i’m trying to create a form which has dropdown menus for certain items  
> and text boxes for others. i have a feeling i’m trying way too hard to  
> make this happen. i have a method which writes strings of the proper  
> tags, which are then brought to life by eval() in the view. the strings  
> are correct, but something about how eval parses the string only allows  
> for one argument, even if i parenthesize them. i feel like i should not  
> have to be using eval at all.

Put the view related code into the view instead of the controller:

Controller:

def run  
@script = @@scriptinfo.getTest(params[:choose][:scriptname])  
end

View:

\<% form\_for :scriptparams, :url =\> {:action =\> “results”} do |f| %\>  
\<% @script.parameters.each do |param| %\>  
\<% if param.values -%\>  
\<%= f.select param.name, param.values %\>  
\<% else -%\>  
\<%= f.text\_field param.name %\>  
\<% end -%\>  
  
  
\<% end %\>  
\<% end %\>

If you do this in more than one place, then you can factor it out into a  
helper…

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 31, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/dynamically-generating-form-tags/132615/4 "2008-03-31T20:24:51Z")

</div>

thanks a lot for your help with this.

-reid

On Fri, Mar 21, 2008 at 7:02 PM, Mark B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
