Html Rails-为什么HAML显示完整哈希?
视图: 控制器Html Rails-为什么HAML显示完整哈希?,html,ruby,ruby-on-rails-3,haml,Html,Ruby,Ruby On Rails 3,Haml,视图: 控制器 !!! %html %head %title= full_title(yield(:title)) =stylesheet_link_tag "application", media: "all" =javascript_include_tag "application" =csrf_meta_tags =render 'layouts/shim' %body =render 'layouts/header'
!!!
%html
%head
%title= full_title(yield(:title))
=stylesheet_link_tag "application", media: "all"
=javascript_include_tag "application"
=csrf_meta_tags
=render 'layouts/shim'
%body
=render 'layouts/header'
.container
=flash.each do |key, value|
%div{class: "alert alert-#{key}"} #{value}
不管flash(:success或:error或其他)如何,它总是将整个哈希编译为html(如下所示)
输出:
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "This is Correct"
redirect_to @user
else
flash[:wrong] = "no"
render 'new'
end
end
…
不
{:错=“否”}
我不知道为什么会显示
{:error=“no”}
。我已经盯着这个终点站好几个小时了。有趣的是,散列是用容器
id输出的,但不是在警报
类中。这感觉像是一个缩进问题,但我经历了几个排列,但没有成功。调用每个块时,需要使用-
而不是=
:
<!DOCTYPE html>
…
<div class='container'>
<div class='alert alert-wrong'>no</div>
{:wrong=>"no"}
</div>
</body>
</html>
从:
还可以将Ruby代码嵌入到Haml文档中。等号=,将输出代码的结果。连字符,-将运行代码,但不会输出结果
因此,您看到散列是因为=
将输出每个块的结果(散列本身,即{:error=>“no”}
)。不是块的最后一个结果。@matt你说得对!谢谢你的更正,更新了我的答案。
-flash.each do |key, value|
%div{class: "alert alert-#{key}"} #{value}