0

我正在使用 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)
  }
  )

这里有任何红宝石向导可以帮助我吗?

4

2 回答 2

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 添加为键的值: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”) ?

于 2013-10-24T03:05:34.717 回答
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) 
  }
  )
于 2013-10-25T01:54:25.063 回答