我有一個錦標賽記錄有很多池。我希望能夠以angularjs生成池,然後當用戶批准它們時,它們將被髮送到rails後端以創建並保存到數據庫中。如何通過在活動記錄中創建的angularjs發送對象集合?
我很難找到一個很好的解釋如何做到這一點(我懷疑我不知道在搜索中使用正確的語言)。
在routes.rb中
resources :tournaments, only: [:create, :index, :show] do
resources :fencers, only: [:show, :create] do
end
resources :pools, only: [:show, :create, :destroy] do
end
collection do
put '/create_pools' => 'pools#create_pools'
end
end
在池控制器
class PoolsController < ApplicationController
def create_pools
tournament = Tournament.find(params[:tournament_id])
tournament.pools.destroy_all # destroy existing pools.
params["pools"].each do |p|
tournament.pools.create(comment_params)
end
respond_with tournament, tournament.pools
end
private
def pool_params
#params.require(:pool).permit(:number, :strip, :fencers)
params.permit(:pool, array: [:number, :strip, :fencers])
end
end
在一個角服務
o.savePools = function(id, pools) {
return $http.post('/tournaments/' + id + '/create_pools.json', pools);
};
我就怎麼做了許多方面很困惑這個的。 1)如何讓控制器接收池的集合? js是否需要像{pools:[pool1,pool2等]}一樣通過它?然後我會通過params [「pools」]訪問它?
2)如何使用集合強打字?我不確定在給定集合中強制鍵入每個單獨池的正確方法。
3)如果「fencers」屬性本身就是另一個對象。我還需要其他更改嗎?
謝謝!請讓我知道,如果有什麼我可以澄清。