0

I'm pretty new to rails and haml. I've got a breadcrumb that I want to render with partials and I want to pass a variable to the partial so that the correct item gets the "enabled" class. Here's what I tried:

= render 'nav_breadcrumb', :locals => {:page1 => true}

and on my _nav_breadcrumb.html.haml page I have this:

.overlay-breadcrumb.clearfix
  %span{:class => ("enabled" if :page1)}
    %span 1
  %span{:class => ("enabled" if :page2)}
    %span 2
  %span{:class => ("enabled" if :page3)}
    %span 3

The problem is, all 3 are getting the enabled class, regardless of the given render variable.

Matt Coady
  • 3,418
  • 5
  • 38
  • 63

1 Answers1

1

Check out this part in the Rails documentation: http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables

Looks like when you pass the variable, you need to write {page1: true}, and then in the template just use the name to refer to the variable.

= render 'nav_breadcrumb', :locals => {page1: true}

.overlay-breadcrumb.clearfix
    %span{:class => ("enabled" if page1)}
      %span 1
    %span{:class => ("enabled" if page2)}
      %span 2
    %span{:class => ("enabled" if page3)}
      %span 3
Steve Baroti
  • 188
  • 7
  • Cool, Thanks! I got a error still but changing 'render' to 'render partial' seems to fix that. Then it complained that page2 and page3 didn't exist so I changed the locals to: {page1: true, page2: false, page3: false} and it worked. Is there a more elegant way than added all the false items as well? – Matt Coady Nov 12 '14 at 00:29