gpt4 book ai didi

ruby-on-rails - has_and_belongs_to_many 关系的 Rails 路由

转载 作者:数据小太阳 更新时间:2023-10-29 08:44:38 25 4
gpt4 key购买 nike

我正在为一支有球员和球队的运动队开发 Rails 4 API,我在 Rails 路由和 has_many 关系方面遇到了一些困难。我的球员和球队之间的关系是这样的:

class Team < ActiveRecord::Base
extend Searchable
validates :title, presence: true
has_and_belongs_to_many :players
end

class Player < ActiveRecord::Base
extend Searchable
validates :first_name, presence: true
validates :last_name, presence: true
has_and_belongs_to_many :teams
end

我希望能够将现有玩家添加到团队中,但我不确定如何更改我的 routes.rb 文件。目前,它看起来像:

Rails.application.routes.draw do
devise_for :users
namespace :api, defaults: { format: :json },
constraints: { subdomain: 'api' }, path: '/' do
scope module: :v1 do
resources :users, :only => [:show, :create, :update, :destroy]
resources :teams, :only => [:show, :create, :update, :destroy, :index]
resources :players, :only => [:show, :create, :update, :destroy, :index]
resources :sessions, :only => [:create, :destroy]
end
end
end

允许对球员和球队模型进行 CRUD 操作。我当时在想,要将一名球员添加到现有球队,我的路线需要如下所示:

/teams/:team_id/add_player/

但我不确定如何在 routes.rb 中声明该路由。所以,有几个问题:

  1. 从 REST-ful 的角度来看,这条路线对人们有意义吗?如果是这样,我将如何在 routes.rb
  2. 中声明这条路线
  3. 将玩家添加到团队应该是 PATCH 还是 POST 方法?

感谢您提供的任何帮助,

肖恩

最佳答案

您需要使用 nested resource :

#config/routes.rb
resources :teams, only: [....] do
resources :players, path: "", path_names: {new: "add_player", create: "add_player", destroy: "remove_player" }, only: [:create, :destroy] #-> /v1/teams/:team_id/add_player
end

这将路由到您的 players_controller , 通过 :team_id参数:

#app/controllers/players_controller.rb
class PlayersController < ApplicationController
def new
@players = Player.all
end

def create
if params[:team_id]
@team = Team.find params[:team_id]
@player = Player.find params[:id]
@team.players << player
else
# normal "create" action
end
end

def destroy
if params[:team_id]
@team = Team.find params[:id]
@player = Player.find params[:id]
@team.players.delete player
end
end
end

您可以将其与以下 View 相结合:

#app/views/players/new.html.erb
<%= form_tag team_players_path do %> #-> method should be "post"
<%= collection_select :player, :id, @players, :id, :name %>
<%= submit_tag %>
<% end %>

--

琐事:

使用 HABTM,您可以获得 << & collection.delete方法——都允许您非常简单地从集合中添加和删除对象。

-

Does that route make sense to people from a REST-ful perspective? If so, how would I declare this route in routes.rb

是的。

Should it be a PATCH or a POST method for adding a player to a team?

根据足智多谋的路由结构,我会说你可以逃脱 POST请求 create行动,但可以通过多种方式完成!


更新

这样做:

#config/routes.rb
resources :teams, only: [....] do
match "players/:id", to: "players", via: [:put, :delete]
end

这将创建以下路由:

#put      url.com/teams/:team_id/players/:id
#destroy url.com/teams/:team_id/players/:id

这将允许您使用 players teams 中的方法 Controller :

#app/controllers/teams_controller.rb
class TeamsController < ApplicationController
def players
@team = Team.find params[:team_id]
@player = Player.find params[:id]
if request.put?
@team.players << player
elsif request.delete?
@team.players.destroy player
end
end
end

关于ruby-on-rails - has_and_belongs_to_many 关系的 Rails 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33364584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com