我正在使用Rhomobile并试图为Alert.show_popup的按钮的id和标题动态构建一个哈希值,但我还没有得到它。我希望最终结果实际上是:
Alert.show_popup( {
:message => 'Please Select Appropriate Address:',
:title => 'Get Nearby...',
:icon => :info,
:buttons => [
{'id' => '1', 'title' => 'Address 1'},
{'id' => '2', 'title' => 'Address 2'},
{'id' => '3', 'title' => 'Address 3'},
{'id' => '4', 'title' => 'Address 4'}
],
:callback => url_for(:action => :on_addressidentified_popup)
}
)
我尝试了一些方法,但没有一个方法有效(构建一个看起来像哈希和try_convert等的字符串)。这是我尝试过的最新版本,但似乎很接近,但仍然很遥远:
nearbyaddresses = Rho::JSON.parse(@params['body'])
h = {}
nearbyaddresses.each do |location|
h[intIndex] = {}
h[intIndex][:id] = intIndex.to_s
h[intIndex][:title] = location["Address"].to_s
intIndex = intIndex + 1
end
Alert.show_popup( {
:message => 'Please Select Appropriate Address:',
:title => 'Get Nearby...',
:icon => :info,
:buttons => h,
:callback => url_for(:action => :on_addressidentified_popup)
}
)
这里的任何红宝石巫师可以帮助我吗?
答案 0 :(得分:0)
怎么样
nearby_addresses_list = Rho::JSON.parse(@params['body'])
buttons_list = nearby_addresses_list.collect.each_with_index {|address, i|
{'id' => i, 'title' => address} #not sure if you need to dig into this at all.
}
这应该使buttons_list具有此值
[{'id' => 0, 'title' => nearby_addresses_list[0]},
{'id' => 1, 'title' => nearby_addresses_list[1]}
{'id' => 2, 'title' => nearby_addresses_list[2]}
{'id' => 3, 'title' => nearby_addresses_list[3]}]
如果您希望id以1开头,请将collect语句的正文更改为{'id' => i+1, 'title' => address}
然后只需将buttons_list添加为key:buttons的值。
Alert.show_popup( {
:message => 'Please Select Appropriate Address:',
:title => 'Get Nearby...',
:icon => :info,
:buttons => buttons_list,
:callback => url_for(:action => :on_addressidentified_popup)
})
如果您看到首先提到的所需输出与您所说的代码接近之间存在奇怪之处,那么您可能在代码中使用符号(:id),并在所需输出中使用字符串(“ id“)?
答案 1 :(得分:0)
以下是我如何解决这个问题。像魅力一样......
intIndex = 0
nearbyaddresses = Rho::JSON.parse(@params['body'])
@@nearbyAddresses = nearbyaddresses
button_array = []
nearbyaddresses.each do |location|
opt = {'id' => intIndex.to_s, 'title' => location["Address"] }
button_array << opt
intIndex = intIndex + 1
end
Alert.show_popup( {
:message => 'Please Select Appropriate Address:',
:title => 'Get Nearby...',
:icon => :info,
:buttons => button_array,
:callback => url_for(:action => :getselectedaddress)
}
)