渲染集合不会渲染集合中的所有项目?

时间:2013-09-09 18:45:53

标签: ruby-on-rails

这看起来非常基本,所以我不确定我做错了什么。我有一个@videos集合(调试@ videos.count显示为4),但行<%= render @videos %>仅渲染4个项目中的2个。这是控制器方法(VideosController):

  def index
    @video = Video.new(athlete_id: current_user.id, sport_id: current_athlete_sport.id)
    @videos = current_user.videos_for_sport(current_athlete_sport).order("date DESC")
    debugger
    respond_with @videos
  end

上述<%= render @videos %>行正在呈现的_video部分:

<%= form_for video do |f| %>
  <div class="row edit-video-container <%= video == @videos.pop ? 'last' : '' %>">
    <div class="span4">
      <div class="row">
        <div class="span4 video-thumbnail">
          <%= image_tag video.thumbnail_url || asset_path("video-encoding-placeholder.png"), {alt: "", title: "#{video.name || "My video" + video.id}"} %>
          <div class="video-ribbon">
            <ul>
              <li class="pull-left">
                <%= link_to "#video-player-modal", { data: { toggle: "modal", link: video.mp4_video_url, thumbnail: video.thumbnail_url }, role: "button", class: "video-player-link" } do %>
                  <i class="icon video-play"></i>
                  <p>Play</br>Video</p>
                <% end %>
              </li>
              <li class="pull-left">
                <%= link_to rotate_video_path(video, direction: "ccw"), :data => { :method => "put", :confirm => "Are you sure?", :type => 'json' } do %>
                  <i class="icon video-rotate-left"></i>
                  <p>Rotate</br>Left</p>
                <% end %>
              </li>
              <li class="pull-left">
                <%= link_to rotate_video_path(video, direction: "cw"), :data => { :method => "put", :confirm => "Are you sure?", :type => 'json' } do %>
                  <i class="icon video-rotate-right"></i>
                  <p>Rotate</br>Right</p>
                <% end %>
              </li>
              <li class="pull-left">
                <%= link_to video_path(video), :data => { :method => "delete", :confirm => "Are you sure?", :type => 'json' } do %>
                  <i class="icon video-delete"></i>
                  <p>Delete</br>Video</p>
                <% end %>
              </li>
            </ul>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="span4">
          <%= f.check_box :featured, { checked: video.featured?, class: "autosave checkbox-right", data: { event: 'change' } } %>
          <%= label_tag "video-checkbox-featured-#{video.id}", "Use as featured video?", { class: "checkbox-right-label" } %>
        </div>
      </div>
    </div>
    <div class="span4">
      <div class="row">
        <div class="span4">
          <%= label :video, :name, "Video Name" %>
          <%= f.text_field :name, { class: "span4 autosave"} %>
        </div>
      </div>
      <div class="row">
        <div class="span4">
          <%= label :video, :video_type_id, "Video Type" %>
          <%= f.select:video_type_id, VideoType.all.collect { |vid| [vid.name, vid.id] }, { include_blank: "Choose One", selected: video.video_type_id }, { class: "chosen-select autosave", id: "", data: { event: 'change' } } %>
        </div>
      </div>
      <div class="row">
        <div class="span4">
          <%= label :video, :sport_id, "Video Sport" %>
          <%= f.select :sport_id, current_user.sports.collect { |sp| [sp.name, sp.id] }, { include_blank: "Choose one", selected: video.sport_id }, { class: "chosen-select autosave", id: "", data: { event: "change" } } %>
        </div>
      </div>
      <div class="row">
        <div class="span4">
          <%= label :video, :date, "Date Recorded" %>
          <%= f.text_field :date, {class: "autosave date datePicker span4", value: js_date(video.date) } %>
        </div>
      </div>
      <div class="row">
        <div class="span4">
          <%= label :video, :uniform_number, "Uniform Number" %>
          <%= f.text_field :uniform_number, { class: "autosave span4"} %>
        </div>
      </div>
    </div>
  </div>
<% end %>

同样,在控制器中抛出调试器,或者在<%= render @videos %>行之前的页面上调试显示4个视频,但实际上只有2个被渲染。在这里有点亏!

1 个答案:

答案 0 :(得分:0)

问题在于:

video == @videos.pop

Pop从你的收藏中取出一个成员。你不应该在这里使用它,因为你制动了循环。

如果是这种情况,我想你只能看到你收藏的第一个和最后一个项目。