Hello guys and girls, I have a problem with a method (new), it is
using 100% of CPU for 15/20 seconds. Follow the method below:
kits_controller.rb
def new
@page_title = ‘Add a Kit’
@kit = Kit.new
@num_itens = 16
@kit_inclusions = Array.new
@num_itens.times { @kit_inclusions.push(KitInclusion.new) }
render :action => 'form'
end
def new_do
kit = Kit.new(params[:form])
if kit.save
params[:kit_inclusions].each do |ki|
next if ki[:product_id].empty?
kit_inclusion = KitInclusion.new(ki)
kit_inclusion.kit_id = kit.id
kit_inclusion.save
end
flash[:notice] = 'Kit added!'
redirect_to :action => 'index'
else
flash[:notice] = kit.errors.full_messages.map {|fm| “
li>”}
redirect_to :action => ‘new’
end
end
form.hmtl.erb
<% form_for :form, @kit, :url => { :action => action_do } do |f| -%>
<% @num_itens.times do |t| -%>
<% fields_for “kit_inclusions[]”, @kit_inclusions[t] do |i| -%>
i.collection_select “product_id”, Product.all(:conditions =>
{:available => true}, :order => “category_id, model
ASC”), :id, :display_for_select, {:include_blank => ‘Select…’},
{:onchange => “qntDefault('quantity_#{t}');”} %><%= i.text_field
“quantity”, {:onkeydown => “return noLetters(event);”, :class =>
‘quantity’, :id => “quantity_#{t}”} %>
<% end -%>
<% end -%>
<%= image_submit_tag ‘btn_save.png’ %>
<%= f.hidden_field :id %>
<% end -%>