我有一个表单,我不知道会有多少text_fields。对于表/表单中的每一行,都有文件的基本名称和旁边的隐藏字段,因此它将与2个文本字段params[:lots[key][lot_number][:low_range][:high_range]
一起传入。
一旦我到达控制器,我只有2到3个原始的8组字段。我不确定为什么这些其他领域被遗忘。
我尝试用15个以上的字段进行操作,每次只能获得5-7个。
我得到的信息是正确的,并且格式正确。我总是错过在@video_filenames
循环
查看:
<%= form_tag mass_create_from_media_admin_event_lots_path(@event), :method => :post do %>
<table class='list'>
<th>Lots</th>
<th>Sub Lot Creation</th>
<% if !params[:videos] || params[:videos] == '1' %>
<% @video_filenames.reject(&:blank?).uniq.each do |filename| %>
<% basename = File.basename(filename, '.*') %>
<% unique_key = Time.now.to_f %>
<tr>
<td><%= basename %><%= hidden_field_tag "lots[#{unique_key}][lot_number]", basename %></td>
<td><%= text_field_tag "lots[#{unique_key}][low_range]" %> <%= text_field_tag "lots[#{unique_key}][high_range]" %></td>
</tr>
<% end %>
<% end %>
</table>
<%= submit_tag "Create Lots" %>
<% end %>
控制器:
def mass_create_from_media
@event = Event.find(params[:event_id])
lots = params[:lots]
abort(params.to_s)
errors = []
lots.each do |key , lot|
if @event.lots.where(:number => lot[:lot_number]).empty?
new_lot = @event.lots.new(:number => lot[:lot_number], :start_at => Time.now, :end_at => Time.now + 1.hour)
if !new_lot.save
errors << new_lot.errors.full_messages
else
if lot[:low_range].present? && lot[:high_range].present?
index = lot[:low_range]
while lot[:high_range].to_i >= index.to_i
sub_lot = @event.lots.new(:number => lot[:lot_number] + '-' + index.to_s, :start_at => Time.now, :end_at => Time.now + 1.hour)
index = index.to_i + 1
sub_lot.save
end
end
end
end
end
if errors.present?
flash[:error] = "Errors creating lots: #{errors.join(', ')}"
else
flash[:success] = "Lots successfully created"
end
redirect_to mass_new_admin_event_lots_path(@event)
end
HTML输出:
<input id="lots_1369421745.947125_lot_number" name="lots[1369421745.947125][lot_number]" type="hidden" value="101z">
<input id="lots_1369421745.947125_lot_number" name="lots[1369421745.947125][lot_number]" type="hidden" value="115z">
<input id="lots_1369421745.947125_lot_number" name="lots[1369421745.947125][lot_number]" type="hidden" value="161z">
<input id="lots_1369421745.948125_lot_number" name="lots[1369421745.948125][lot_number]" type="hidden" value="20z">
<input id="lots_1369421745.948125_lot_number" name="lots[1369421745.948125][lot_number]" type="hidden" value="20z">
<input id="lots_1369421745.948125_lot_number" name="lots[1369421745.948125][lot_number]" type="hidden" value="47z">
<input id="lots_1369421745.948125_lot_number" name="lots[1369421745.948125][lot_number]" type="hidden" value="87z">
<input id="lots_1369421745.948125_lot_number" name="lots[1369421745.948125][lot_number]" type="hidden" value="rock_on">
文字输入:
<input id="lots_1369421745.948125_high_range" name="lots[1369421745.948125][high_range]" type="text">
<input id="lots_1369421745.948125_low_range" name="lots[1369421745.948125][low_range]" type="text">
答案 0 :(得分:0)
问题是<% unique_key = Time.now.to_f %>
没有生成唯一键。正如您在html中看到的那样,重复了1369421745.947125
和1369421745.948125
键。
因此,您必须更改“算法”以生成唯一键。
答案 1 :(得分:0)
我将<% unique_key = Time.now.to_f %>
更改为<% unique_key = SecureRandom.urlsafe_base64 %>
答案 2 :(得分:0)
试试这个
<% @video_filenames.reject(&:blank?).uniq.each_with_index do |filename, index| %>
<% basename = File.basename(filename, '.*') %>
<tr>
<td><%= basename %><%= hidden_field_tag "lots[#{index}][lot_number]", basename %></td>
<td><%= text_field_tag "lots[#{index}][low_range]" %> <%= text_field_tag "lots[#{index}][high_range]" %></td>
</tr>
<% end %>
<% end %>