Mims H. Wright
Mims H. Wright

Reputation: 3037

Nested methods in Ruby Helper

I'm working on a project in MiddleMan. Trying to pass the results of a method as the parameters of another method. I'm running into all kinds of syntax issues with the commas and parens. Everything looks right to me, am I crazy? Thanks in advance!

The method call that I'm having trouble with is create_campus_subnav()

module SubnavHelper

  def create_subnav_link ( label, link="" )
    if link == ""
      link = label
    end
    return "          <li id='item-#{link}'><a class='title' id='link-#{link}'>#{label}</a></li>"
  end

  def create_subnav (*links)
    subnav = "\
    <nav class='nav-sub'>\
      <div class='nav-wrap'>\
        <ul class='nav-list'>"

    links.each do |attr,val|
      subnav += " attr: " + attr;
      subnav += " val: " + val;
      # subnav += args["attr"] + "\n"
    end 

    subnav += "\
        </ul>\
      </div>\
    </nav>"
    return subnav
  end

  def create_campus_subnav
      subnav = create_subnav ( 
        create_subnav_link ("Campus Overview", "Overview"), 
        create_subnav_link ("About", "About"),  
        create_subnav_link ("Admissions"), 
        create_subnav_link ("Career Services" "CareerServices"),       
        create_subnav_link ("Schedules, Tuition & Fees" "Tuition"),         
        create_subnav_link ("Financial Assistance & Scholarships" "FinancialAssistance"),         
        create_subnav_link ("Faculty")      
      )
      puts subnav
  end
end

And the errors:

/dev/config.rb:54:in `require': /gia/dev/helpers/subnav_helper.rb:36: syntax error, unexpected ',', expecting ')' (SyntaxError)
        create_subnav_link ("Campus Overview", "Overview"), 
                                              ^
/dev/helpers/subnav_helper.rb:36: syntax error, unexpected ')', expecting keyword_end
        create_subnav_link ("Campus Overview", "Overview"), 
                                                          ^
/dev/helpers/subnav_helper.rb:37: syntax error, unexpected ',', expecting ')'
        create_subnav_link ("About", "About"),  
                                             ^
/dev/helpers/subnav_helper.rb:37: syntax error, unexpected ')', expecting keyword_end
        create_subnav_link ("About", "About"),  
                                                      ^
/dev/helpers/subnav_helper.rb:39: syntax error, unexpected tLPAREN_ARG, expecting keyword_do or '{' or '('
        create_subnav_link ("Career Services" "...
                            ^
/dev/helpers/subnav_helper.rb:39: syntax error, unexpected ',', expecting keyword_end
...er Services" "CareerServices"),       
...                               ^
/dev/helpers/subnav_helper.rb:41: syntax error, unexpected tLPAREN_ARG, expecting keyword_do or '{' or '('
        create_subnav_link ("Financial Assistance & Schol...
                            ^
/dev/helpers/subnav_helper.rb:41: syntax error, unexpected ',', expecting keyword_end
...rships" "FinancialAssistance"),         
...                               ^
/dev/helpers/subnav_helper.rb:43: syntax error, unexpected ')', expecting keyword_end

Upvotes: 0

Views: 225

Answers (2)

brentmc79
brentmc79

Reputation: 2541

Try using the built-in view helpers, like content_tag instead of doing a bunch of string concatenation:

def subnav(*links)
  content_tag(:nav, 
    content_tag(:div,
      content_tag(:ul,
        links.map do |link|
          content_tag :a, link.text, :href => link.href do
        end.join, :class => 'nav-list'),
      :class => 'nav-wrap'
    ),
    :class => 'nav-sub'
  )
end

Note: I just took a guess at the structure of your 'links' variable, but I think you get the idea

Upvotes: 0

vgoff
vgoff

Reputation: 11313

def create_subnav (*links)
  subnav = "\
    <nav class='nav-sub'>\
      <div class='nav-wrap'>\
        <ul class='nav-list'>"

        links.each do |attr,val|
          subnav += " attr: " + attr;
          subnav += " val: " + val;
          # subnav += args["attr"] + "\n"
        end 

        subnav += "\
        </ul>\
      </div>\
    </nav>"
    return subnav
end

def create_campus_subnav
  subnav = create_subnav(
                          create_subnav_link("Campus Overview", "Overview"),
                          create_subnav_link("About", "About"),
                          create_subnav_link("Admissions"),
                          create_subnav_link("Career Services" "CareerServices"),
                          create_subnav_link("Schedules, Tuition & Fees" "Tuition"),
                          create_subnav_link("Financial Assistance & Scholarships" "FinancialAssistance"),
                          create_subnav_link("Faculty")
                         )
                         puts subnav
end

This will do it for you, but I think I would prefer the first entry to the argument list to be on the same line.

Upvotes: 1

Related Questions