0

我在 MiddleMan 做一个项目。试图将一个方法的结果作为另一个方法的参数传递。我遇到了逗号和括号的各种语法问题。一切对我来说都是正确的,我疯了吗?提前致谢!

我遇到问题的方法调用是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

和错误:

/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
4

2 回答 2

1
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

这将为您完成,但我想我希望参数列表的第一个条目位于同一行。

于 2012-11-15T03:28:59.817 回答
0

尝试使用内置的视图助手,content_tag而不是做一堆字符串连接:

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

注意:我只是猜测了您的“链接”变量的结构,但我认为您明白了

于 2012-11-15T05:22:27.383 回答