未定义的方法`any?'为零:NilClass 2

时间:2016-09-23 10:01:18

标签: ruby-on-rails video methods

我正在尝试在我的应用中显示所有视频,所以当我添加新视频并想要重定向到movie_path时,我就在我的视频控制器中执行此操作:

def create
  @video = Video.new(video_params)
  if @video.save
    flash[:success] = 'Video added!'
    redirect_to movie_path(@movies)
  else
    render :new
  end
end

它给了我一个错误: 未定义的方法`any?'为零:NilClass 这是我想要显示视频的节目页面:

<% if @videos.any? %>
 <div class="container">
  <div id="player-wrapper"></div>
   <% @videos.in_groups_of(3) do |group| %>
    <div class="row">
     <% group.each do |video| %>
      <% if video %>
        <div class="col-md-4">
          <div class="yt_video thumbnail">
            <%= link_to image_tag("https://img.youtube.com/vi/#{video.uid}/mqdefault.jpg", alt: video.title,
                                  class: 'img-rounded'),
                        "https://www.youtube.com/watch?v=#{video.uid}", target: '_blank' %>
            <div class="caption">
              <h5><%= video.title %></h5>
              <p>Published at <%= video.published_at.strftime('%-d %B %Y %H:%M:%S') %></p>
              <p>
                <span class="glyphicon glyphicon glyphicon-thumbs-up"></span> <%= video.likes %>
                <span class="glyphicon glyphicon glyphicon-thumbs-down"></span> <%= video.dislikes %>
              </p>
            </div>
          </div>
        </div>
      <% end %>
    <% end %>
  </div>
<% end %>

这是电影控制器:

def index
  @movies = Movie.all.order(:cached_votes_score => :desc)
  @movies = @movies.paginate(:page => 1, :per_page => 8)

end

def show
  @reviews = Review.where(movie_id: @movie.id).order("created_at DESC")
end

def new
  @movie = current_user.movies.build
  @movie = Movie.new 
  @categories = Category.all.map{|c| [ c.name, c.id ] }
end

这是我的路线:

enter image description here

2 个答案:

答案 0 :(得分:3)

nil没有方法any?所以你必须保护你的代码免受

的影响

而不是这一行:

<% if @video.any? %>

写下这个:

<% if @video.try(:any?) %>

答案 1 :(得分:2)

我认为你没有定义.root{ -fx-background-color: #2D2E32; -fx-font-size: 11px; } #file{ -fx-background-color: #3E3F43; } #file .label{ -fx-text-fill: #EAEAEA; } .context-menu{ -fx-background-color: #3E3F43; } #closeButton, #minimizeButton, #maximizeButton{ -fx-background-radius: 0; -fx-background-color: #3E3F43; -fx-text-fill: #ffffff; -fx-font-weight: bold; } #closeButton:hover{ -fx-background-color: #E46458; } #minimizeButton:hover{ -fx-background-color: #80B1E0; } #maximizeButton:hover{ -fx-background-color: #80E089; } ,你定义了@videos,所以你应该试试这个:

@video