Forum: Ruby on Rails Multiple Row Form similar to Milestones on BaseCamp

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.
Be80899dae082db4ae633c4706312609?d=identicon&s=25 Phil Wilson (tankow)
on 2006-01-24 00:47
Hi all,

Wondering how to setup my view and controller to do something similar to
the form on Basecamp that allows adding up to 10 milestones on one page.

Thanks
Phil
992fe8c19bbbc27f2b562a9f96efc03d?d=identicon&s=25 Jamis Buck (Guest)
on 2006-01-24 01:09
(Received via mailing list)
On Jan 23, 2006, at 4:47 PM, Phil Wilson wrote:

> Hi all,
>
> Wondering how to setup my view and controller to do something
> similar to
> the form on Basecamp that allows adding up to 10 milestones on one
> page.

The trick, Phil, is to do something like:

   <% 10.times do |n| %>
     <%= text_field_tag "milestones[#{n}][title]" %><br />
   <% end %>

Then, in your controller you do:

   def create
     10.times do |n|
       next if params[:milestones][n.to_s][:title].blank?
       Milestone.create(params[:milestones][n.to_s])
     end
   end

Hope that makes sense,

Jamis
Be80899dae082db4ae633c4706312609?d=identicon&s=25 Phil Wilson (tankow)
on 2006-01-24 01:44
I've really got to start thinking along simpler terms!  This is great.
Would extending this for multiple fields be simply checking each field
to see if it is blank or something like this?

>    def create
>      10.times do |n|
>        next if params[:milestones][n.to_s].empty?
>        Milestone.create(params[:milestones][n.to_s])
>      end
>    end

Thanks very much.
Phil

Jamis Buck wrote:
> On Jan 23, 2006, at 4:47 PM, Phil Wilson wrote:
>
>> Hi all,
>>
>> Wondering how to setup my view and controller to do something
>> similar to
>> the form on Basecamp that allows adding up to 10 milestones on one
>> page.
>
> The trick, Phil, is to do something like:
>
>    <% 10.times do |n| %>
>      <%= text_field_tag "milestones[#{n}][title]" %><br />
>    <% end %>
>
> Then, in your controller you do:
>
>    def create
>      10.times do |n|
>        next if params[:milestones][n.to_s][:title].blank?
>        Milestone.create(params[:milestones][n.to_s])
>      end
>    end
>
> Hope that makes sense,
>
> Jamis
This topic is locked and can not be replied to.