ruby-on-rails - Haml 格式

标签 ruby-on-rails haml

我是 haml 的新手,所以我仍在尝试弄清楚格式。

我有一个包含以下代码的 index.haml 文件。

%h1
  Welcome to Solidarity

Hello,
= @profile.first_name
!

它呈现如下:

Welcome to Solidarity
Hello, user !



这是页面源:
<h1>
  Welcome to Solidarity
</h1>
Hello,
frances
!

@profile.first_name 和感叹号之间有一个空格。这是为什么?而且,我该如何解决?

最佳答案

%h1 Welcome to Solidarity
Hello, #{@profile.first_name}!
Please #{link_to 'post a comment', new_comment_path}!

变成
<h1>Welcome to Solidarity</h1>
Hello, John!
Please <a href="/comments/new">post a comment</a>!

请记住,在 Rails 2 和 Haml 2 中,您必须正确地对发送到浏览器的任何内容进行 html 转义 (ht nex3):
Hello, #{h @profile.first_name}!

在 Rails 3 和 Haml 3 中,默认情况下所有内容都被转义,因此您可以简单地执行以下操作:
Hello, #{@profile.first_name}!

关于ruby-on-rails - Haml 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2711070/

相关文章:

ruby-on-rails - rails : how to get all key-values from Rails. 缓存

ruby-on-rails - 如何为生产环境运行docker compose

javascript - 必须双击选项卡才能获取选项卡内容

javascript - 使用 haml 在 RoR 中以确认形式运行 Controller 操作

ruby - 在 haml 中创建一个 th 标签,其 id 是一个 Ruby 变量

ruby-on-rails - 在通过自定义初始值设定项编写的 config 目录中的自定义 yml.erb 文件中使用 image_path 帮助程序

ruby-on-rails - 如何在 Ruby 中检索 SOAP 调用和响应?

ruby-on-rails - 将redirect_to与特定的ActiveRecord对象一起使用来创建到该对象的链接

ruby-on-rails - Rails/Haml : How to create a post form?

html - HAML Twitter Bootstrap 选项卡不能正常工作 Ruby on Rails