【问题标题】:Syntax error for link in nested content_tag :div嵌套 content_tag 中链接的语法错误:div
【发布时间】:2012-11-13 04:16:35
【问题描述】:

我是 Rails 新手。我正在尝试添加指向使用 content_tag 生成的 div 的链接。我遇到语法错误,但我似乎无法弄清楚原因。

我用过这个reference

- flash.each do |name, msg|
  - if msg.is_a?(String)
    = content_tag :div, msg content_tag(:a, "Close", :href => '', :class => 'close'), :id => "flash_#{name}", :class => 'alert-box alert'

我做错了什么?

【问题讨论】:

  • 你正在尝试产生类似<div>msg <a>Close</a></div>的东西?
  • 是的。我应该提到这一点。谢谢。

标签: ruby-on-rails ruby-on-rails-3.2 haml


【解决方案1】:

您忘记将msg 与您的content_tag :a 连接起来。试试这个:

- flash.each do |name, msg|
  - if msg.is_a?(String)
    = content_tag :div, 
                  "#{msg} #{content_tag(:a, 'Close', :href => '', :class => 'close')}",
                  :id => "flash_#{name}", :class => 'alert-box alert'

提示: 你可以use a blockcontent_tag 来提高可读性。

content_tag :div do
  # the content of this block will be captured
end

【讨论】:

  • 谢谢!我确实必须将其设为 html_safe。请更新您的示例,以防另一个 n00b 遇到此问题;)
猜你喜欢
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多