【发布时间】:2020-05-10 10:15:54
【问题描述】:
所以我有一个使用我的 displayPlayerObject 函数来显示播放器的组件。问题是我没有正确的路线来获取函数和我的routes.rb中的POST。模型关系有球员属于球队,获得球员的路线是“http://localhost:3000/api/teams/1/players/1” 1 是前者的球队ID,后者的球员ID。但是如何让 displayPlayerObject 以同样的方式工作呢? routes.rb 中的 POST 应该是什么样子?另外我怀疑我的玩家控制器的“显示”也是错误的。
displayPlayerObject 函数(已编辑):
export const displayPlayerObject = (id, teamId, type) => {
return dispatch => {
const data = { id };
fetch(`/api/teams/:team_id/players/show`, {
method: 'post',
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(data)
})
.then(res => res.json())
.then(responseJSON => { dispatch({ type , player_object: responseJSON})
})
}
};
我的 routes.rb(已编辑):
Rails.application.routes.draw do
scope '/api' do
post "teams/show", to: 'teams#show'
post "teams/:team_id/players/show", to: 'players#show'
resources :teams do
resources :players
resources :star_players
end
end
end
玩家控制器展示(已编辑):
def show
Player.find(params[:id])
render json: @player, status: 200
end
【问题讨论】:
标签: javascript ruby-on-rails nested-routes