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) %>   </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" />   </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 -- Posted via http://www.ruby-forum.com/.
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) %> >   > </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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060221/402cb276/attachment.html
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> 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 > >------------------------------------------------------------------------ > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060221/509f2293/attachment.html
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" />   </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 -- Posted via http://www.ruby-forum.com/.