【问题标题】:rails app wont append youtube links with "embedd"rails 应用程序不会发送带有“嵌入”的 youtube 链接
【发布时间】:2014-11-04 03:06:09
【问题描述】:

我正在关注此 sitepoint moviestore tutorial,除了在用户购买电影后视频未加载到 iframe 中之外,一切正常。我有一个 csv 文件,其中有一列包含 youtube 网址,用于测试应用支付系统(使用 Braintree)。

查看 html 的源代码,youtube 链接显示没有嵌入部分,因此它从 db 文件中查找信息,但它只是没有将嵌入输入到链接中。实际上,我确实设法通过在 CSV 中的 youtube 链接中手动输入“/embed/”来制作视频。

我对 Rails 很陌生,但如果有人能解释为什么它对我不起作用,我将不胜感激。

这就是我的 movie.rb 模型的样子:

class Movie < ActiveRecord::Base
has_many :purchases
has_many :buyers, through: :purchases

before_save :embed_video_url

def poster
    "http://ia.media-imdb.com/images/M/#{poster_url}"
end

def imdb
    "http://www.imdb.com/title/#{imdb_id}/"
end

def embed_video_url
 self.video_url = "//www.youtube.com/embed/#{video_url.split('v=')[1].split('&list')[0]}"
end

def cart_action(current_user_id)
 if $redis.sismember "cart#{current_user_id}", id
   "Remove from"
else
  "Add to"
end
end     
end

视图包含 iframe

<%if signed_in?%>
<%if current_user.purchase? @movie %>
  <div class="flex-video">
    <iframe title="YouTube video player" width="100%" height="" src="<%= @movie.video_url %>" frameborder="0" allowfullscreen></iframe>
  </div>
<%else%>
  <%=link_to "", class: "button", data: {target: @cart_action, addUrl: add_to_cart_path(@movie), removeUrl: remove_from_cart_path(@movie)} do%>
    <i class="fi-shopping-cart"></i>
    <span><%=@cart_action%></span> Cart
  <%end%>
<%end%>

这就是电影控制器的样子

class MoviesController < ApplicationController
 def index
  @movies = Movie.all
 end

 def show
  @movie = Movie.find(params[:id])
  @cart_action = @movie.cart_action current_user.try :id
 end
end

提前致谢!

【问题讨论】:

    标签: javascript ruby-on-rails ruby iframe youtube


    【解决方案1】:

    embed_video_url 方法中你使用 video_url 应该是 self.video_url

    您的方法可能无法处理所有情况,因此请检查Converting a regular Youtube 'link' into an embedded video 问题。

    或者您可以自动获取 embeded_url,然后查看 Video Info gem。支持youtubevimeoDailymotionVK

    【讨论】:

    • 感谢您的回复。但如果你再看一遍,你会发现我实际上在embed_video_url 中使用了self.video_url。还是您指的是在其他地方调用它?
    • @MCrolio video_url.split 是在没有self 的情况下调用它的地方
    • 对不起,我不关注。叫它在哪里?没有自我,我在叫什么。当我查看代码时,我所做的正是您所指的
    • self.video_url = "//www.youtube.com/embed/#{video_url.split('v=')[1].split............ ..
    【解决方案2】:

    对于遇到类似问题的任何人。来自another SO post 的 Douglas F Shearer 的解决方案似乎已经解决了这个问题。需要一点修改。这很可能不是最干净的解决方案,但请参见下文...

    将此添加到 movie_helper。

    def youtube_embed
      youtube_url = @movie.video_url
      if youtube_url[/youtu\.be\/([^\?]*)/]
        youtube_id = $1
      else
        youtube_url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/]
        youtube_id = $5
      end
    
     %Q{<iframe title="YouTube video player" width="100%" height="390" src="http://www.youtube.com/embed/#{ youtube_id }" frameborder="0" allowfullscreen></iframe>}
    end
    

    我设置youtube_url = @movie.video_url 以从 CSV 中检索 video_url。

    然后在电影视图中简单地调用它......

    <p><%= raw(youtube_embed()) %></p>
    

    我必须添加 raw() 以防止 iframe 被呈现为字符串。

    但是当代码在模型中时它不会工作。视图无法识别youtube_embed()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-07
      • 2012-02-20
      • 2020-10-19
      • 1970-01-01
      • 2013-06-16
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多