【问题标题】:Padrino content_for rendering string instead of HTMLPadrino content_for 渲染字符串而不是 HTML
【发布时间】:2014-11-27 00:44:49
【问题描述】:

我有一个Sinatra 应用程序并且正在使用Padrino。当我使用 content_for 帮助器在布局中呈现内容时,它会将其作为字符串而不是 html 返回。

app.rb

class CateringApp < Sinatra::Application
  register Padrino::Helpers
end

layout.erb

<div id="page-wrapper">
  <%= flash[:notice] %>
  <%= content_for?(:main_table) ? yield_content(:main_table) : yield %>
</div>

category.erb

<% content_for :main_table do %>
  <div id="name" class="row">
    "IM ALIVE"
  </div>
<% end %>

我在浏览器上得到的结果是字符串

<div id="name" class="row">
   "IM ALIVE"
</div>

app.rb

require 'dotenv'
Dotenv.load

require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/reloader'
require 'sinatra/flash'
require 'sinatra/assetpack'
require 'padrino-helpers'

class CateringApp < Sinatra::Application
  enable :protect_from_csrf
  use Rack::Session::Cookie, :secret => ENV['SESSION_SECRET']
  register Padrino::Helpers
  register Sinatra::AssetPack
end

# require all config files
Dir["./config/*.rb"].each {|file| require file }
require './auth'

Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    activemodel (4.1.6)
      activesupport (= 4.1.6)
      builder (~> 3.1)
    activerecord (4.1.6)
      activemodel (= 4.1.6)
      activesupport (= 4.1.6)
      arel (~> 5.0.0)
    activesupport (4.1.6)
      i18n (~> 0.6, >= 0.6.9)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.1)
      tzinfo (~> 1.1)
    arel (5.0.1.20140414130214)
    backports (3.6.0)
    builder (3.2.2)
    carrierwave (0.10.0)
      activemodel (>= 3.2.0)
      activesupport (>= 3.2.0)
      json (>= 1.7)
      mime-types (>= 1.16)
    dotenv (0.11.1)
      dotenv-deployment (~> 0.0.2)
    dotenv-deployment (0.0.2)
    excon (0.39.6)
    fog (1.23.0)
      fog-brightbox
      fog-core (~> 1.23)
      fog-json
      fog-softlayer
      ipaddress (~> 0.5)
      nokogiri (~> 1.5, >= 1.5.11)
    fog-brightbox (0.5.1)
      fog-core (~> 1.22)
      fog-json
      inflecto
    fog-core (1.24.0)
      builder
      excon (~> 0.38)
      formatador (~> 0.2)
      mime-types
      net-scp (~> 1.1)
      net-ssh (>= 2.1.3)
    fog-json (1.0.0)
      multi_json (~> 1.0)
    fog-softlayer (0.3.19)
      fog-core
      fog-json
    formatador (0.2.5)
    i18n (0.6.11)
    inflecto (0.0.2)
    ipaddress (0.8.0)
    jsmin (1.0.1)
    json (1.8.1)
    mail (2.5.4)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    mime-types (1.25.1)
    mini_portile (0.6.0)
    minitest (5.4.1)
    multi_json (1.10.1)
    net-scp (1.2.1)
      net-ssh (>= 2.6.5)
    net-ssh (2.9.1)
    nokogiri (1.6.3.1)
      mini_portile (= 0.6.0)
    padrino-helpers (0.12.3)
      i18n (~> 0.6, >= 0.6.7)
      padrino-support (= 0.12.3)
      tilt (~> 1.4.1)
    padrino-support (0.12.3)
      activesupport (>= 3.1)
    pg (0.17.1)
    polyglot (0.3.5)
    pony (1.11)
      mail (>= 2.0)
    rack (1.5.2)
    rack-protection (1.5.3)
      rack
    rack-test (0.6.2)
      rack (>= 1.0)
    sinatra (1.4.5)
      rack (~> 1.4)
      rack-protection (~> 1.4)
      tilt (~> 1.3, >= 1.3.4)
    sinatra-activerecord (2.0.2)
      activerecord (>= 3.2)
      sinatra (~> 1.0)
    sinatra-assetpack (0.3.3)
      jsmin
      rack-test
      sinatra
      tilt (>= 1.3.0, < 2.0)
    sinatra-contrib (1.4.2)
      backports (>= 2.0)
      multi_json
      rack-protection
      rack-test
      sinatra (~> 1.4.0)
      tilt (~> 1.3)
    sinatra-flash (0.3.0)
      sinatra (>= 1.0.0)
    sinatra-reloader (1.0)
      sinatra-contrib
    thread_safe (0.3.4)
    tilt (1.4.1)
    treetop (1.4.15)
      polyglot
      polyglot (>= 0.3.1)
    tzinfo (1.2.2)
      thread_safe (~> 0.1)

PLATFORMS
  ruby

DEPENDENCIES
  activerecord
  carrierwave
  dotenv
  fog
  padrino-helpers
  pg
  pony
  sinatra
  sinatra-activerecord
  sinatra-assetpack
  sinatra-flash
  sinatra-reloader

【问题讨论】:

  • 期望的输出是什么?对我来说,这看起来像是正确的行为。还是您错过了布局和类别中的内容?如果您出于调试目的删除 content_for,会发生什么情况?布局中的线?
  • @Felix 我得到的是字符串 '
    ' 等,而不是实际渲染一个 div。
  • 看起来 Padrino::Rendering 由于某种原因未激活。您能否提供一个带有锁定 Gemfile 的最小失败项目?
  • @kittyminky2:如果您从模板中排除 content_for/yield 部分,您是否设法仅获得呈现的布局?
  • @Felix 我还有其他在布局中正常呈现的视图。但是带有 content_for 块的 category.erb 视图呈现为字符串而不是 html。我有另一个我认为相关的未解决问题,与我的 form_fors 未正确呈现有关

标签: ruby sinatra padrino


【解决方案1】:

在您的 Gemfile 中包含 gem 'erubis'。

只有当相应的 gem(erubis、haml 或 slim)包含在包中并且在需要 'padrino-helpers' 时可见时,才能激活 Padrino::Rendering。

【讨论】:

  • 添加 erubis gem 刚刚破坏了我所有的 sinatra 资产包链接!
  • 你可以在assetpack助手上调用.html_safe来将它们标记为html。
猜你喜欢
相关资源
最近更新 更多
热门标签