Rails在Do循环中复制值

时间:2015-06-26 00:17:56

标签: ruby-on-rails forms loops

我有一个do循环,可以查找用户关于某个事件的所有记录。

events_rsvp架构有 : 用户身份 :EVENT_ID :shift_id :日期

使用shift_id和日期是可选的。如果某个活动发生了变化,他们可以注册。事件发生的所有日子都会重复转换,因此他们最终会在不同日期注册相同的名单。这就是问题出现的地方。

如果用户在多天内注册某个活动,则不会复制数据,只需在多天内注册相同的标题班次。

以下是相关的观看代码:

<%= form_tag(event_submit_hours_for_volunteers_path(@event.id), :method => :post) do -%>
    <% @volunteers_hours_for_dates.reverse.each_with_index do |volunteers_hours_for_date, index| %>
    <h3>
        Day #<%= @total_event_dates.reverse.index(volunteers_hours_for_date[:date]) + 1 %> - <%= volunteers_hours_for_date[:date].strftime("%B #{volunteers_hours_for_date[:date].day.ordinalize}, %Y") %>
    </h3>
    <table class="add_hours_to_volunteer_table">
        <thead>
            <tr>
                <th>
                    <%= check_box_tag "select_all_#{index}", true %>
                    <%= label_tag "select_all_#{index}", "Attended?" %>
                </th>
                <th colspan="2">
                    Volunteer
                </th>
                <th>
                     Shift
                </th>
                <th>
                    Registered Hours
                </th>
                <th>
                    <%= link_to "Assign all &raquo;".html_safe, "#", :title => "Assign all", :class => "assign_all_link"  %>
                </th>
                <th>
                    Actual Hours
                </th>
            </tr>
        </thead>
        <tbody>
            <% if volunteers_hours_for_date[:volunteers_hours_info].empty? %>
                <tr>
                    <td colspan="7">
                        No volunteers for this event yet!
                    </td>
                </tr>
            <% end %>
            <% volunteers_hours_for_date[:volunteers_hours_info].each do |volunteer_hours_info| %>
                <tr>
                    <td>
                        <%
                            should_row_be_selected = false
                            should_row_be_selected = true if params["user_#{volunteer_hours_info[:user].id}_#{volunteers_hours_for_date[:date].strftime("%d_%m_%Y")}"]
                            should_row_be_selected = true if volunteer_hours_info[:amount].to_f > 0
                        %>
                        <%= check_box_tag "user_#{volunteer_hours_info[:user].id}_#{volunteers_hours_for_date[:date].strftime("%d_%m_%Y")}", true, should_row_be_selected %>
                    </td>
                    <td>
                        <%= image_tag volunteer_hours_info[:user].avatar_url, :size => "40x40" %>
                    </td>
                    <td>
                        <%= volunteer_hours_info[:user].first_name %> <%= volunteer_hours_info[:user].last_name %>
                    </td>
                    <td>
                        <% if not volunteer_hours_info[:shifts].empty? %>
                            <%= volunteer_hours_info[:shifts].collect{|s| s.position}.join(", ") %>
                        <% else %>
                            N/A
                        <% end %>
                    </td>
                    <td class="registeredhours">
                        <% if not volunteer_hours_info[:shifts].empty? %>
                            <%= number_with_precision(volunteer_hours_info[:volunteering_total_hours], :precision => 2, :strip_insignificant_zeros => true) %>
                        <% else %>
                            <%= Event::DEFAULT_VOLUNTEERING_HOURS %>
                        <% end %>
                    </td>
                    <td>
                        <%= link_to "Assign &raquo;".html_safe, "#", :class => "assign_hours_link", :title => "Assign"  %>
                    </td>
                    <td>
                        <%= text_field_tag "amount_#{volunteer_hours_info[:user].id}_#{volunteers_hours_for_date[:date].strftime("%d_%m_%Y")}", volunteer_hours_info[:amount], :style => "width:75px; #{volunteer_hours_info[:is_amount_valid] ? nil : 'border:solid 1px red;'}", :class => "add_hours_for_volunteer_text_field", :autocomplete => :off %>
                    </td>
                </tr>
            <% end %>

以及相关的控制器代码:

def add_hours_for_volunteers
    @event = Event.includes(:rsvp_users).group("users.id").find(params[:event_id])
    event_rsvps = @event.event_rsvps
    authorize! :add_event_hours, @event.organizer
    @volunteers_hours_for_dates = []
    @end_date_to_use = (@event.end_date and @event.end_date <= Date.today) ? @event.end_date : Date.today
    @total_event_dates = @event.get_occuring_dates(@event.start_date, @end_date_to_use, nil, true)
    get_dates_for_current_page.each do |event_date|
        volunteers_hours_info = []
        @event.rsvp_users.each do |user|
            volunteers_hours_info.push get_volunteer_hours_info(user, event_date) if !event_rsvps.collect{|r| r if r.date == event_date and r.user == user}.compact.empty?
        end 
        @volunteers_hours_for_dates.push({:date => event_date, :volunteers_hours_info => volunteers_hours_info})
    end
end

以下是它正在做的事情的截图。你可以看到它如何结合两个班次并总结小时数,但仍然在两天内显示它。

screenshot of issue

到目前为止,我试着玩这个循环而没有运气。

0 个答案:

没有答案