Mateusz Urbański
Mateusz Urbański

Reputation: 7862

Set class and data in HAML based on condition

I have some partial that looks like this:

.btn.btn-success{class: 'disabled',"data-original-title" => "Tooltip on left", "data-placement" => "left", "data-toggle" => "tooltip"}
  %span.fa.fa-gear
  Process

Is there possibility to refactor this template to set this:

{class: 'disabled',"data-original-title" => "Tooltip on left", "data-placement" => "left", "data-toggle" => "tooltip"}

only if for example 1+1 == 2?

I was trying almost everything but it not works...

Upvotes: 0

Views: 545

Answers (2)

user4384619
user4384619

Reputation:

It's an example to give the class active, depending controller and method: You can use it to create your own.

application_helper.rb

module ApplicationHelper
  def is_active(controller, action)       
    params[:action] == action && params[:controller] == controller ? "active" : nil        
  end 
end

view file

 %li
   %a{class: is_active('controller_1', 'index'), :href => "/controller_1"}

Upvotes: 0

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

You can do that easily

class: ('disabled' if  1 + 1 == 2), "data-original-title" => ("Tooltip on left" if  1 + 1 == 2), "data-placement" => ("left" if  1 + 1 == 2), "data-toggle" => ("tooltip" if  1 + 1 == 2)

Upvotes: 1

Related Questions