2013-10-24 2 views
0

Rhomobile을 사용 중이며 Alert.show_popup에있는 버튼의 ID와 제목에 대한 해시를 동적으로 만들려고 시도했지만 실제로 얻지는 못했습니다. 나는 것으로 최종 결과를 원하는 것은, 사실상이다JSON에서 루비로 다차원 배열 또는 해시 만들기

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

해시를 원하십니까? 'buttons'는 첫 번째 예제에서 배열입니다. –

답변

0

당신이 ID의가 1로 시작 {'id' => i+1, 'title' => address}

에 수집 문장의 본문을 변경하려는 경우에 대한

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. 
} 

이이 값

[{'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]}] 

와 buttons_list 남겨 두어야 방법 그런 다음 버튼의 값으로 buttons_list를 추가하기 만하면됩니다. (: ID) 당신이 먼저 언급 원하는 출력과 가까이했다 코드 사이의 불확실성가 표시되는 경우

Alert.show_popup({ 
    :message => 'Please Select Appropriate Address:', 
    :title => 'Get Nearby...', 
    :icon => :info, 
    :buttons => buttons_list, 
    :callback => url_for(:action => :on_addressidentified_popup) 
    }) 

, 당신은 당신의 코드에서 키에 대한 상징을 사용하는 것이 아마도 그것을이며, 원하는 출력 ("id")의 문자열?

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) 
    } 
)