samedi 27 juin 2015

Rails / Bootstrap / HAML - How to convert this code to display flash messages to HAML?

I want to convert the following code to HAML to handle Bootstrap's alert messages in a Rails 4.2.2 application. I've tried manually, using html2haml and online converters and the code I get never works.

The code:

<div class="alert 
    <%= 
    case type.to_sym 
    when :alert, :danger, :error, :validation_errors
        'alert-danger'
    when :warning, :todo
        'alert-warning'
    when :notice, :success
        'alert-success'
    else 
    'alert-info'
    end
    %>
     alert-dismissible" role="alert">
    <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
    <%= content %>
</div>

This is what I get from converters:

.alert.case.when.:validation_errors.when.:todo.when.:success.else.end.alert-dismissible{:class => "<haml_loud> type.to_sym :alert, :danger, :error, 'alert-danger' :warning, 'alert-warning' :notice, 'alert-success' 'alert-info' </haml_loud>", :role => "alert"}
  %button.close{"data-dismiss" => "alert", :type => "button"}
    %span{"aria-hidden" => "true"} ×
    %span.sr-only Close
  = content

I know it's ugly but it is the only code I have found that works out of the box with Bootstrap 3.5.5. If anyone has suggestions for new code using HAML, I'm open to hear.

Aucun commentaire:

Enregistrer un commentaire