0

我有两个模型:图像和视频。图片有时属于视频,但并非总是如此,但视频始终属于图片(这是因为我为上传到网站的视频生成缩略图,但用户也可以上传常规图片有相关的视频)。

在我看来,我浏览了所有图像并检查是否有任何图像具有关联的视频。如果是这样,那么它应该根据视频的属性“embed_url”创建一些东西。

当我尝试运行代码时,我不断收到错误消息:

undefined method `embed_url' for nil:NilClass

我不明白为什么,所以我做了 if 语句:if(false)",即使代码永远不会到达 if 语句的那部分,我仍然会收到错误。我做错了什么?

<% step.images.order("position").each_with_index do |image, index| %>

    <% Rails.logger.debug "image #{image.id}"%>

      <script type="text/javascript">
      <% has_video = !image.video.blank? %>

      <% Rails.logger.debug "image #{image.id} has_video: #{has_video}"%>
       var has_video = Boolean(<%=has_video%>);

        // add step images to carousel
        if(<%=index%>==0){

          if(false){
              $('.carousel-inner.<%=step.id%>').append('<div class="item active">
              <div class="flex-video"> <a class="fancybox" href="<%=**image.video.embed_url**%>" rel="gallery <%=step.id%>" data-fancybox-type="iframe"><iframe src="<%=image.video.embed_url%>" id="<%=image.id%>"></iframe></a></div></div>'); 
          }
          else{
           $('.carousel-inner.<%=step.id%>').append('<div class="item active"><a class="fancybox" href="<%=image.image_path_url%>" rel="gallery <%=step.id%>" data-fancybox-type="image"> <%=image_tag(image.image_path_url(:preview), :width => "100%", :id=> image.id)%></a></div>');    
          } 
...
      <% end %>
4

1 回答 1

2

Your if(false) is Javascript, not Ruby - while the template is being rendered, it's not going to have any bearing on what actually gets executed. Anything in <% ... %> is what gets run serverside, which is what you're having issues with here.

于 2013-07-10T21:28:47.223 回答