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.
Phil W. (Guest)
on 2006-01-24 01: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
Jamis B. (Guest)
on 2006-01-24 02:09
(Received via mailing list)
On Jan 23, 2006, at 4:47 PM, Phil W. 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
Phil W. (Guest)
on 2006-01-24 02: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 B. wrote:
> On Jan 23, 2006, at 4:47 PM, Phil W. 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.