Must be a cleaner way to do this

Am trying to do a bulk entry facility for some project or other. It
works but it feels ugly. I generate a form with twenty empty input
fields:

%= form_tag(“action” => “enter_stuff”) %>
<% (1…20).each do |line| %>


<%= line %>
<<%= text_field(“action”, “name”, “size” => 80) %>
&nbsp

<% end %>
<%= submit_tag(“Enter stuff”) %>
<%= end_form_tag %>

That creates twenty lines of html that looks like this:

2 < &nbsp

The action enter_stuff looks like this

  breakpoint('in main.enter_stuff')
  request.params['action[name]'].each do |name|
      if name != ""
        action = Action.new
        action.name = name
        action.save
      end
  end

Can’t use @params. In the console @params looks like

{"commit=>“Enter stuff”, “action”=>“enter_stuff”, “controller”=>“main”)

Is there a cleaner way to do this?

Ed

First thing, that (1…20) smells like business logic, so I’d move it to
the
controller. That makes the show controller look like this -

def show
@actions = []
(1…20).eash do |line|
@actions << Action.new
end
end

Then in your form, use a partial
<%= form_tag(“action” => “enter_stuff”) %>
<%= render :partial => “action”, :collection => @actions %>
<%= submit_tag(“Enter stuff”) %>

In _action.rhtml -

        <div id="line<%= line %>">
        <%= line %>
        <<%= text_field("action[#{action_counter}]", "name", "size" =>
  1. %>
    &nbsp

now, when this is submitted, you get back a 2D array like
action[1][name]=xxx

def process
params[:action].each do |action|
if !action[:name].nil?
Action.create(action)
end
end
end

Also, I think that test for nil actions isn’t necessary, as empty fields
don’t get submitted with the form.

All of this is a brain dump, sans tests, but something like it should
work.

matt

This looks pretty good, but I would check for blank? instead of nil?
here, as blank text fields do get submitted with the form.

def process
params[:action].each { |action| Action.create(action) unless
action.blank? }
end

the blank? check is equal to (var.nil? || var.empty?)

Cheers!
Patrick

Mathew, Patrick –

I like the look of these better than what I have – but they don’t work.
In the partial, I am getting this error –

action[0] is not allowed as an instance variable name

As I understand it, the rendering is passing each invocation of the
partial a single element and the array reference ruins its day.

Have tried various permutations, and the least bad seems to be to have
the line in the partion look like

<%= text_field(“action”, “name”, :index => action_counter, “size” => 80)
%>

This gives me html that looks like this

1 &nbsp
,

params that look like this (in the process action)

params =>
{“commit”=>“Enter stuff”, “action”=>“enter_stuff”, “controller”=>“main”}

and request.params that look like this

request.params =>
{“action[19][name]”=>[""], “action[15][name]”=>[""], “commit”=>[“Enter
stuff”], “action[8][name]”=>[""], “action[1][name]”=>[“another lame “],
“action[17][name]”=>[””], “action[13][name]”=>[""],
“action[7][name]”=>[""], “action[0][name]” =>[“this is yet”],
“action[12][name]”=>[""],
“action[9][name]”=>[""], “action[5][name]”=>[""],
“action[14][name]”=>[""],
“action[10][name]”=>[""], “action[6][name]”=>[""],
“action[2][name]”=>[“test”], “action[18][name]”=>[""],
“action[11][name]”=>[""],
“action[4][name]”=>[""], “action[16][name]”=>[""],
“action[3][name]”=>[""]}

which i could probably process if I wanted to, but I want my darn
@params to look right.

Any ideas? I’m getting close to deciding the form helper is more
trouble than its worth in this contect. Am I off base?

Thanks
Ed