Railtie(gem):我如何包含和渲染动词部分?



我正在开发一个Railtie(gem),用于嵌入视频,如YouTube, Vimeo等。

在这个gem中,我想有一个视图助手,这样我就可以调用embed_video(embeddable, width, height)

所以我已经创建了一个助手作为一个助手,它的工作,但我想重构它使用部分,使它更干净,但我发现很难做到这一点。

助手:

module ViewHelpers
  def embed_video(embeddable, width, height)
    if embeddable.video_on_youtube?
      content_tag :iframe, nil, width: width, height: height,
        src: "//www.youtube.com/embed/#{embeddable.video_id}",
        allowfullscreen: true, frameborder: 0
    elsif embeddable.video_on_vimeo?
      content_tag :iframe, nil, width: width, height: height,
        src: "//player.vimeo.com/video/#{embeddable.video_id}",
        webkitallowfullscreen: true, mozallowfullscreen: true,
        allowfullscreen: true, frameborder: 0
    elsif embeddable.video_on_dailymotion?
      content_tag :iframe, nil, width: width, height: height,
        src: "//www.dailymotion.com/embed/video/#{embeddable.video_id}",
        webkitallowfullscreen: true, mozallowfullscreen: true,
        allowfullscreen: true, frameborder: 0
    elsif embeddable.video_on_veoh?
      %Q{
        <object width='#{width}' height='#{height}' id='veohFlashPlayer' name='veohFlashPlayer'>
          <param name='movie' value='http://www.veoh.com/swf/webplayer/WebPlayer.swf?version=AFrontend.5.7.0.1446&permalinkId=#{embeddable.video_id}&player=videodetailsembedded&videoAutoPlay=0&id=anonymous'></param>
          <param name='allowFullScreen' value='true'></param>
          <param name='allowscriptaccess' value='always'></param>
          <embed src='http://www.veoh.com/swf/webplayer/WebPlayer.swf?version=AFrontend.5.7.0.1446&permalinkId=#{embeddable.video_id}&player=videodetailsembedded&videoAutoPlay=0&id=anonymous' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='#{width}' height='#{height}' id='veohFlashPlayerEmbed' name='veohFlashPlayerEmbed'>        </embed>
        </object>
      }.html_safe
    #
    # And more providers...
    #
  end
end

使用偏导,我想这样写:

module ViewHelpers
  def embed_video(embeddable, width, height)
    if embeddable.video_on_youtube?
      render 'youtube', embeddable: embeddable, width: width, height: height
    elsif embeddable.video_on_veoh?
      render 'veoh'#...etc
    end
  end
end

我尝试的解决方案:

  • 创建文件夹: lib/可嵌入/泛音

  • 为veoh添加偏导: lib/可嵌入/泛音_veoh.html.erb

在railtie.rb中添加视图路径

module Embeddable
  class Railtie < Rails::Railtie
    # ...load view helpers... #
    initializer 'embeddable.add_autoload_paths', :before => :set_autoload_paths do |app|
      app.config.autoload_paths << app.root.join("lib/embeddable/partials").to_s
    end
    # My attempt at adding the partials to the view path
    initializer 'embeddable.add_view_paths', :after => :add_view_paths do |app|
      ActiveSupport.on_load(:action_controller) do
        append_view_path app.root.join("lib/embeddable/").to_s
      end
    end
  end
end

当我尝试渲染视图时,我得到以下错误:

ActionView::MissingTemplate: Missing partial partials/veoh with {:locale=>[:nb], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :rabl, :haml]}. Searched in:
  * "/home/me/Arbeid/the_application/app/views"
  * "/home/me/.rvm/gems/ruby-2.1.0/gems/kaminari-0.15.1/app/views"
  * "/home/me/Arbeid/the_application/lib/embeddable"

经过大量的跟踪和错误,我设法让它工作。这是我必须做的:

我改了这个:

initializer 'embeddable.add_view_paths', :after => :add_view_paths do |app|
  ActiveSupport.on_load(:action_controller) do
    append_view_path app.root.join("lib/embeddable/").to_s
  end
end

这:

initializer 'embeddable.add_view_paths', :after => :add_view_paths do |app|
  ActiveSupport.on_load(:action_controller) do
    append_view_path "#{Gem.loaded_specs['embeddable'].full_gem_path}/lib/embeddable"# <- this is the change
  end
end

现在我可以像这样从帮助器中渲染部分:

render 'partials/youtube', id: video_id, width: width, height: height

我知道这不是你问题的直接答案,但我宁愿建议使用AutoHTML gem:

auto_html是一个Rails扩展,用于将url转换为合适的url资源(图片,链接,YouTube, Vimeo视频,…)。这是完美的如果您不想用富HTML编辑器或标记代码,但你仍然想让他们嵌入视频,图像,在你的网站上粘贴链接和更多的内容。

https://github.com/dejan/auto_html

直接回答你的问题,我认为问题是你混淆了自动加载路径和视图路径。

你应该有

app.config.autoload_paths << app.root.join("lib/embeddable").to_s

append_view_path app.root.join("lib/embeddable/partials").to_s

相关内容

  • 没有找到相关文章

最新更新