我正在MiddleMan开展一个项目。试图将方法的结果作为另一个方法的参数传递。我遇到了逗号和parens的各种语法问题。一切看起来都对我,我疯了吗?提前谢谢!
我遇到问题的方法调用是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
答案 0 :(得分: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
这会为你做,但我想我更希望参数列表的第一个条目在同一行。
答案 1 :(得分: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
注意:我只是猜测了'links'变量的结构,但我认为你明白了这个想法