【问题标题】:Rendering a partial in assets渲染部分资产
【发布时间】:2011-09-07 15:01:15
【问题描述】:

我正在使用 Ruby on Rails 3.1,我想知道如何在 javascript 资产中呈现部分内容。

我的目标:

# in /app/assets/javascript/cart.js.coffee.erb
$('a.add_sth').click -> $('.random_container').append('<%= render partial: 'way/to/partial' %>')

这会导致 NoMethodError:

undefined method `render' for #<#<Class:0x007fc54584c6e8>:0x007fc5474cd470>

如果我改写 &lt;%= 2+3 %&gt;,它就可以正常工作,顺便说一句。

我认为问题在于资产管道独立于默认的 ActionView,这就是为什么 render() 在那里是未知的。无论如何,有没有办法渲染部分内容?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1 asset-pipeline


    【解决方案1】:

    坏消息,渲染不可用 看:same question on GitHub

    【讨论】:

      【解决方案2】:

      请记住,资产适用于静态数据,例如 CSS、JS 或不会动态更改其内容的图像,因此可以更好地缓存和/或导出到 CDN。

      由于您可以使用 ruby​​ 代码运行 ERB,它应该始终返回相同的值(因为它只会在编译资产时执行)。

      这就是我猜 render 在资产内部不可用的原因(尽管它可以正确用于渲染静态数据)。

      这里的简单解决方案:将您的 JS 文件移动到视图中,在那里您将能够使用任何视图助手。

      【讨论】:

        【解决方案3】:

        这对我有用。 (对于 HAML)

        = Haml::Engine.new(File.read(File.join(Rails.root, 'app/views/xxxxx','_form.html.haml'))).render(Object.new, :hello => "Hello World")
        

        并且,需要在要更新的文件开头添加依赖项,例如: 在这种情况下,需要依赖的文件在资产中。

        //= depend_on xxxxx/_form.html.haml
        

        【讨论】:

        • 由于某些原因render 在末尾添加了一个新行,使 js 代码无效。通过在render 之后添加chomp 修复
        【解决方案4】:

        在 Rails 4.2 中

        我找到了这个帖子https://github.com/sstephenson/sprockets/issues/90 这建议使用

        这对我有用。

        【讨论】:

          【解决方案5】:

          我有类似的问题,所以我写了这个 render 方法,可以在资产内部使用来渲染 ERB 部分模板:

          # in lib/my_app/erb_helpers.rb
          module MyApp
            module ERBHelpers
              class << self
          
                def render(partial_path, binding)
                  dir_name, _, partial_name = partial_path.rpartition(File::SEPARATOR)
                  file_name = "_#{partial_name}.html.erb"
                  Erubis::Eruby.new(File.read(File.join(Rails.root, 'app', 'views', dir_name, file_name)).gsub("'", %q(\\\'))).result(binding)
                end
          
              end
            end
          end
          

          然后我在 coffeescript 文件中使用它,如下所示:

          # in app/assets/javascripts/notifications.coffee
          MyApp.notifications.templates =
            notice: '<%= ::MyApp::ERBHelpers.render 'application/notifications/notice', content: "%content%" %>'
            alert: '<%= ::MyApp::ERBHelpers.render 'application/notifications/alert', content: "%content%" %>'
          
          MyApp.notifications.create_elem = (type, content) -> MyApp.notifications.templates[type].replace('%content%', content)
          

          PS:我在 Rails 5.0 应用上测试过

          【讨论】:

            【解决方案6】:

            事实上,它对我有用。 你需要做的:

            = render 'way/to/partial'
            

            其中 'way/to/partial' 是现有资产文件夹下的相对路径。 有线的事情是在路径中,您需要省略资产下的第一级文件夹。

            【讨论】:

            • 什么?怎么可能给方法提供不同的参数以使其可用?您在某处有工作示例吗?
            猜你喜欢
            • 2017-10-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-01-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多