Dynamically generating form tags

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 %>

would returning a lambda object work?

On Fri, Mar 21, 2008 at 2:04 PM, Reid O.
[email protected]

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…

thanks a lot for your help with this.

-reid

On Fri, Mar 21, 2008 at 7:02 PM, Mark B.
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs