在Sinatra中将哈希作为实例变量传递给Get方法

时间:2014-08-24 19:16:55

标签: ruby sinatra

我为项目创建了一个metro应用程序。我试图传递一个哈希,其中包含用户可以选择作为键的选项以及它将重定向到的值作为值。当我这样做时,sinatra报道:

没有将Array隐式转换为String

这很奇怪,因为到目前为止我的应用程序中没有数组。我已经尝试在index.erb文件中注释掉嵌入式ruby,但这并没有帮助。申请如下:

require "sinatra"
require "sinatra/reloader"

red = {
    "Woodley Park" => '/woodley_park',
    "Dupont Circle" => '/dupont_circle',
    "Farragut North" => '/farragut_north',
    "Metro Center" => '/metro_center',
    "Union Station" => '/union_station'
}

turquoise = {
    "Crystal City" => '/crystal_city',
    "Metro Center" => '/metro_center',
    "Shaw-Howard" => '/shaw_howard',
    "Beltwater" => '/beltwater'
}

orange = {
    "Farragut West" => '/farragut_west',
    "McPherson" => '/mcpherson',
    "Metro Center" => '/metro_center',
    "Federal Triangle" => '/federal_triangle',
    "Smithsonsian" => '/smithsonsian',
    "L'efant Plaza" => '/lefant_planza'
}

lines = {
    "Red" => "/red_line",
    "Turquoise" => "/turquoise_line",
    "Orange" => "/orange_line"
}

get '/' do
    @header = "Welcome to the D.C. Metro! Which line will you be starting at?"
    @options = lines
end

index.erb看起来像这样。再次,评论相关的行并没有改变结果。提前谢谢!

<h2><%= @header %></h2>

<% @options.each do |name, link| %>
<a href=<%= link %>><%= name %></a>
<% end %>

0 个答案:

没有答案