Forum: Ruby on Rails must be a cleaner way to do this ...

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ed Schechter (Guest)
on 2006-02-21 08:28
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

%= form_tag("action" => "enter_stuff") %>
	<% (1..20).each do |line| %>
	    <div id="line<%= line %>">
	    <%= line %>
	    <<%= text_field("action", "name", "size" => 80) %>
	<% end %>
	<%= submit_tag("Enter stuff") %>
<%= end_form_tag %>

That creates twenty lines of html that looks like this:

<div id="line2">
    <<input id="action_name" name="action[name]" size="80" type="text"

The action enter_stuff looks like this

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

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?

matthew clark (Guest)
on 2006-02-21 09:28
(Received via mailing list)
First thing, that (1..20) smells like business logic, so I'd move it to
controller.  That makes the show controller look like this -

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

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" =>
> 80) %>
>             &nbsp
>             </div>

now, when this is submitted, you get back a 2D array like

def process
  params[:action].each do |action|
    if !action[:name].nil?

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

Patrick R. (Guest)
on 2006-02-21 16:10
(Received via mailing list)
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? }

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

Ed Schechter (Guest)
on 2006-02-22 09:00
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

<div id="line1" >
     <input id="action_1_name" name="action[1][name]" size="80"
type="text" />

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[9][name]"=>[""], "action[5][name]"=>[""],
 "action[10][name]"=>[""], "action[6][name]"=>[""],
"action[2][name]"=>["test"], "action[18][name]"=>[""],
"action[4][name]"=>[""], "action[16][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?

This topic is locked and can not be replied to.