我基本上在我的team
和user
模型之間有以下關係。 # team.rb has_many :users
我如何設置一條路線從團隊中刪除has_many:用戶
在team#show頁面上,我有一張用戶表,我想包含一個鏈接,以便從團隊中刪除每個用戶。我目前不確定我應該如何設置路線,因爲我從未遇到過。
會喜歡一些這方面的幫助:)在此先感謝!
我基本上在我的team
和user
模型之間有以下關係。 # team.rb has_many :users
我如何設置一條路線從團隊中刪除has_many:用戶
在team#show頁面上,我有一張用戶表,我想包含一個鏈接,以便從團隊中刪除每個用戶。我目前不確定我應該如何設置路線,因爲我從未遇到過。
會喜歡一些這方面的幫助:)在此先感謝!
將此添加到您的routes.rb。
resources :teams do
resources :users
end
您將獲得航線範圍的由team_id
如:用於刪除
delete /teams/:team_id/users/:id
你也可以讓用戶的個人資源
資源:用戶
你會獲取路線爲
delete user users/:id
而在你的用戶控制器,增加破壞行動
class UsersController < ApplicationController::Base
def destroy
@user = User.find(params[:id])
if @user.destroy
flash[:notice] = "User deleted successfully"
else
flash[:error] = "Problem deleting user"
end
redirect_to teams_path
end
end
我假設你不想刪除用戶,只需刪除其隸屬關係與他們目前的球隊。我還假設你想要一名球員一次只能成爲一支球隊的一員。只要有每個鏈接旁邊的一個團隊成員鏈接到用戶#更新
class UsersController < ApplicationController::Base
def update
user = User.where(:id => params[:id])
user.update(params[])
user.save
end
end
只要你在你的routes.rb文件說resources :users
長,你可以用它使您的默認更新路由。只要確保你傳遞了你想要的新參數(如果你想刪除一個外鍵,它將會是零)。您可以使用它來添加,刪除或更新模型中的任何列。
我得到了這個而不是'''/:id''' '''DELETE/teams /:team_id/users(。:format)''' –
@GuoXiangTan:你做'耙路線'? – usha
我的不好:P我輸入'''resource'''而不是'''resources''' :) –