视图中的不需要的Flash消息

时间:2014-09-09 16:15:29

标签: ruby-on-rails

我有这个代码用于在HAML文件中显示Flash消息:

# View partial
Test1

= flash.each do |type, message|
  .container
    .row
      .col-md-12        
        %div{class: "alert #{bootstrap_class_for(type)} alert-dismissible", role: 'alert'}
          Test1a
          = message
          Test1b
          %button.close{:'data-dismiss' => 'alert', type: 'button'}
            %span{:'aria-hidden' => 'true'} ×
            %span.sr-only Close

Test2

我无法理解为什么在Test1b和Test2之间会显示一个简单的哈希。

enter image description here

如果有帮助,可以使用以下代码:

# Helpers
# http://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages
module DeviseHelper
  def devise_error_messages!
    if resource.errors.full_messages.any?
      flash.now[:error] = resource.errors.full_messages.join(' & ')
    end
    return
  end
end

module ApplicationHelper
  # https://gist.github.com/roberto/3344628
  def bootstrap_class_for flash_type
    case flash_type.to_sym
      when :success
        "alert-success"
      when :error
        "alert-danger"
      when :alert
        "alert-warning"
      when :notice
        "alert-info"
      else
        flash_type.to_s
    end
  end
end

# view
.container-fluid
  .row
    .full-width-background.form-page
    .col-md-4.col-md-offset-4
      = form_for(resource, as: resource_name, url: registration_path(resource_name), html: {class: 'form-page'}) do |f|
        = devise_error_messages!
        .form-group
          = f.label :email
          = f.email_field :email, autofocus: true, class: 'form-control'
        .form-group
          = f.label :password
          - if @validatable
            %i #{@minimum_password_length} characters minimum
          = f.password_field :password, autocomplete: 'off', class: 'form-control'
        .form-group
          = f.label :password_confirmation
          = f.password_field :password_confirmation, autocomplete: 'off', class: 'form-control'
        .form-group
          = f.submit 'Sign up', class: 'btn btn-primary'

        = render 'devise/shared/links'

1 个答案:

答案 0 :(得分:1)

因为你有:

= flash.each

相反:

- flash.each

在HAML中,=意味着获取Ruby的结果并将其放在页面上(因此,当您迭代某些内容时,结果是您迭代的集合),以及{{1}意味着只评估Ruby。