0

我有一个表单,我不知道会有多少 text_fields。对于表格/表单中的每一行,都有文件的基本名称,以及旁边的隐藏字段,因此它与 2 个文本字段一起传入,params[:lots[key][lot_number][:low_range][:high_range].

一旦我到达控制器,我只有原来的 8 组字段中的 2 到 3 组。我不确定为什么这些其他领域被抛在后面。

我尝试使用 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">
4

3 回答 3

0

问题是<% unique_key = Time.now.to_f %>没有生成 uniq 密钥。正如您在 html 中看到的那样,键1369421745.9471251369421745.948125重复。

所以,你必须改变你的“算法”来生成 uniq 密钥。

于 2013-05-24T18:05:09.613 回答
0

<% unique_key = Time.now.to_f %> 改为<% unique_key = SecureRandom.urlsafe_base64 %>

于 2013-05-24T18:10:25.867 回答
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 %>
于 2013-05-24T18:12:39.090 回答