我需要一些帮助打印我的哈希值.在我的"web.rb"文件中,我有:
class Main < Sinatra::Base
j = {}
j['Cordovan Communication'] = {:title => 'UX Lead', :className => 'cordovan', :images => ['http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150']}
j['Telia'] = {:title => 'Creative Director', :className => 'telia', :images => ['http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150']}
get '/' do
@jobs = j
erb :welcome
end
end
Run Code Online (Sandbox Code Playgroud)
在"welcome.rb"中我打印哈希值,但它不起作用:
<% @jobs.each do |job| %>
<div class="row">
<div class="span12">
<h2><%=h job.title %></h2>
</div>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这是我的错误消息:
NoMethodError at / undefined method `title' for #<Array:0x10c144da0>
Run Code Online (Sandbox Code Playgroud)
想想@jobs看起来像:
@jobs = {
'Cordovan Communication' => {
:title => 'UX Lead',
:className => 'cordovan',
:images => ['http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150']},
'Telia' => {
:title => 'Creative Director',
:className => 'telia',
:images => ['http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150']}
}
Run Code Online (Sandbox Code Playgroud)
然后记住,each调用哈希将一个键和一个值传递给块,你会看到你有:
@jobs.each do |name, details|
# On first step, name = 'Cordovan Communication', details = {:title => 'UX Lead', ...}
end
Run Code Online (Sandbox Code Playgroud)
所以你想要的可能是:
<% @jobs.each do |name, details| %>
<div class="row">
<div class="span12">
<h2><%=h details[:title] %></h2>
</div>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4853 次 |
| 最近记录: |