首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除带有Rails 3.2的连接表中的行

删除带有Rails 3.2的连接表中的行
EN

Stack Overflow用户
提问于 2012-05-12 18:01:56
回答 1查看 340关注 0票数 0

我有两个模型,用户和帐户,它们通过AccountUsers有一个多对多的关系。用户可以邀请其他用户到他们的帐户,但我也希望通过身份验证的用户能够删除受邀请的用户(或协作者)。我只希望删除连接表中的关联或行,而不是删除user对象。我不太确定如何做到这一点,特别是我应该如何设置我的路由,销毁方法和link_to。

我的方法目前看起来是这样的:

代码语言:javascript
复制
def destroy
    @account.users.delete(collaborator)
end

我的链接看起来像这样:

代码语言:javascript
复制
= link_to "Remove collaborator", collaborator,  confirm: "You sure?", :method => :delete

这目前导致了

代码语言:javascript
复制
undefined method `user_path' for #<#<Class:0x007fe3fc4f2378>:0x007fe3fe718510>

我也曾尝试将@account.users.delete(collaborator)直接放入我的link_to中,但它会在单击该行之前将其删除。

我的路由当前如下所示:

代码语言:javascript
复制
resources :accounts do
  resources :projects
  resources :invitations
  resources :collaborators, :only => [:index]
end

我的模型联想是这样的:

代码语言:javascript
复制
# User
has_many :account_users
has_many :accounts, through: :account_users, :dependent => :destroy

# Account
belongs_to :user
has_many :account_users
has_many :users, through: :account_users

我应该怎么做,怎么做才能实现我想要的?

不是我有一个单独的控制器(协作者),我的销毁操作所在的位置,它不在我的用户控制器中。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-12 18:52:33

当您使用以下命令时,问题可能出在路由上

代码语言:javascript
复制
 resources :collaborators, :only => [:index]

并且还嵌套在帐户中。所以你可以试着重写一下routes.rb

代码语言:javascript
复制
resources :accounts do
  resources :projects
  resources :invitations
  resources :collaborators
end

您的链接应该如下所示

代码语言:javascript
复制
 = link_to 'Remove collaborator', accounts_colaborator_path(@account,@colaborator), :method => :delete
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10562729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档