我有一個數組作爲哈希值:轉換字符串數組成鍵值對
:params=>[":centre_id", ":id"]
我試圖找到一種方法來在陣列中的每個元素轉換爲這樣的事情:
:centre_id => 1
:id => 1
我嘗試不同的方式加載,但我似乎無法找到一個很好的清潔方法來做到這一點:
"#{route[:params].map {|x| x.parameterize.underscore.to_sym => '1'}}"
這是我想實現:
-{"controller"=>"venues", "action"=>"activity, {:centre_id=>1, :id=>1}"}
+{"controller"=>"venues", "action"=>"activity", "centre_id"=>"1", "id"=>"1"} << this one
string or symbol doesn't matter does it?
使用此:
expect(route[:request_method] => route[:path]).to route_to "#{route[:controller]}##{route[:action]}, #{Hash[route[:params].map {|x| [x.sub(/\A:/,'').to_sym, 1] }]}"
幫助將非常感激。
你的輸入數據是什麼樣的? – tadman 2014-10-29 17:47:10
@tadman [{:request_method =>:get,:path =>「/ leisure/1/activity/1」,:controller =>「venues」,:params => [「:centre_id」,「:id」 ],::controller_constant =>「VenuesController」,:action =>「activity」}, – user3868832 2014-10-29 17:55:42
我想實現get(「/ game/1」)。 ) – user3868832 2014-10-29 17:56:37