Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html Rails-为什么HAML显示完整哈希?_Html_Ruby_Ruby On Rails 3_Haml - Fatal编程技术网

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=&gt;&quot;no&quot;}
    </div>
  </body>
</html>
从:

还可以将Ruby代码嵌入到Haml文档中。等号=,将输出代码的结果。连字符,-将运行代码,但不会输出结果


因此,您看到散列是因为
=
将输出每个
块的结果(散列本身,即
{:error=>“no”}
)。

不是块的最后一个结果。@matt你说得对!谢谢你的更正,更新了我的答案。
-flash.each do |key, value|
  %div{class: "alert alert-#{key}"} #{value}