用于销毁 has_many 的视图/控制器:通过关联

发布于 2024-12-23 21:00:53 字数 845 浏览 1 评论 0原文

我有用户、团队和 TeamMemberships,它们与 has_many :through 关联。

我正在尝试添加从团队中删除用户的功能,这需要销毁关联他们的 TeamMembership 模型。

我的模型如下:

# models/team.rb
class Team < ActiveRecord::Base
  has_many :team_memberships, :dependent => :destroy
  has_many :members, :through => :team_memberships
end

# models/user.rb
class User < ActiveRecord::Base
  has_many :team_memberships
  has_many :teams, :through => :team_memberships
end

# models/team_membership.rb
class TeamMembership < ActiveRecord::Base
  belongs_to :team
  belongs_to :member, class_name: 'User', foreign_key: 'user_id'
end

视图当前如下所示:

- @team.members.each do |member|
  .member
    = link_to member.name, user_path(member)
    = button_to "Remove User"

我正在寻求帮助来完成“删除用户”按钮的实现。我不知道如何传递我需要销毁的 TeamMembership。

I've got Users, Teams, and TeamMemberships, which are associated with a has_many :through.

I'm trying add the ability to remove a User from a Team, which requires destroying the TeamMembership model that associates them.

My Models are as follows:

# models/team.rb
class Team < ActiveRecord::Base
  has_many :team_memberships, :dependent => :destroy
  has_many :members, :through => :team_memberships
end

# models/user.rb
class User < ActiveRecord::Base
  has_many :team_memberships
  has_many :teams, :through => :team_memberships
end

# models/team_membership.rb
class TeamMembership < ActiveRecord::Base
  belongs_to :team
  belongs_to :member, class_name: 'User', foreign_key: 'user_id'
end

The view currently looks like this:

- @team.members.each do |member|
  .member
    = link_to member.name, user_path(member)
    = button_to "Remove User"

I'm looking for help with finishing the implementation for the "Remove User" button. I'm not sure how to pass in the TeamMembership that I need to destroy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

峩卟喜欢 2024-12-30 21:00:53
resources :teams do
    resources :users do
        member do
            delete :remove_from_team
        end
    end
end

在用户控制器中:

def remove_from_team
    @team = Team.find(params[:team_id]) #can do that in before_filter
    user = User.find(params[:id])
    @team.members.delete user
    redirect_to @team
end

在视图中,

= button_to "Remove user", remove_from_team_team_user_path(@team, member), :method => :delete

您也可以

resources :team_memberships

在 team_memberships 控制器中执行销毁操作,

= button_to "Remove user", team_membership_path(@team.team_memberships.where(:user_id => member.id).first), :method => :delete

但是即使用户从未单击链接,也需要进行查询才能获取每个成员的 team_membership...

resources :teams do
    resources :users do
        member do
            delete :remove_from_team
        end
    end
end

In the users controller:

def remove_from_team
    @team = Team.find(params[:team_id]) #can do that in before_filter
    user = User.find(params[:id])
    @team.members.delete user
    redirect_to @team
end

In the view

= button_to "Remove user", remove_from_team_team_user_path(@team, member), :method => :delete

You could also just have

resources :team_memberships

And have a destroy action in the team_memberships controller

= button_to "Remove user", team_membership_path(@team.team_memberships.where(:user_id => member.id).first), :method => :delete

But there would be a query needed to get the team_membership for each member even if the user never click the link...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文