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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F5c659335007b04061aa1b84e2992f31?d=identicon&s=25 Ed Schechter (Guest)
on 2006-02-21 07: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
fields:


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

That creates twenty lines of html that looks like this:

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

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
69b2ef4bce76b5b27c94e898976dc6d8?d=identicon&s=25 matthew clark (Guest)
on 2006-02-21 08:28
(Received via mailing list)
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" =>
> 80) %>
>             &nbsp
>             </div>



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
E4dd2cacb0207907f1961d1a76e14141?d=identicon&s=25 Patrick Ritchie (Guest)
on 2006-02-21 15: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? }
end

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

Cheers!
Patrick
F5c659335007b04061aa1b84e2992f31?d=identicon&s=25 Ed Schechter (Guest)
on 2006-02-22 08: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" >
     1
     <input id="action_1_name" name="action[1][name]" size="80"
type="text" />
     &nbsp
</div>,

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
This topic is locked and can not be replied to.