通过删除关联激活管理员 has_many

发布于 2025-01-02 19:57:08 字数 1488 浏览 1 评论 0原文

我目前正在建立这样的关联:

show do
  h3 project.title
  panel "Utilisateurs" do
    table_for project.roles do
      column "Prenom" do |role|
        role.user.firstname
      end
      column "Nom" do |role|
        role.user.lastname
      end
      column "email" do |role|
        role.user.email
      end
      column "Role" do |role|
        role.role_name.name
      end
    end
  end
end

# override default form
form do |f|
  f.inputs "Details" do # Project's fields
    f.input :title
    f.input :code
  end

  f.has_many :roles do |app_f|
    app_f.inputs do
      # if object has id we can destroy it
      if app_f.object.id
        app_f.input :_destroy, :as => :boolean, :label => "Supprimer l'utilisateur du projet"
      end
      app_f.input :user,      :include_blank => false, :label_method => :to_label
      app_f.input :role_name, :include_blank => false 
    end
  end
  f.buttons
end

我有以下关联:

项目

has_many :roles, :dependent => :destroy 
has_many :users, :through => :role

用户

has_many :roles, :dependent => :destroy
has_many :projects, :through => :role 

角色

belongs_to :user
belongs_to :project
belongs_to :role_name

角色名称

has_many :roles

当我尝试时通过我的表单破坏用户关联没有任何反应,有解决这个问题的想法吗? 或者将删除链接添加到我的节目块?

I'm currently making association like this :

show do
  h3 project.title
  panel "Utilisateurs" do
    table_for project.roles do
      column "Prenom" do |role|
        role.user.firstname
      end
      column "Nom" do |role|
        role.user.lastname
      end
      column "email" do |role|
        role.user.email
      end
      column "Role" do |role|
        role.role_name.name
      end
    end
  end
end

# override default form
form do |f|
  f.inputs "Details" do # Project's fields
    f.input :title
    f.input :code
  end

  f.has_many :roles do |app_f|
    app_f.inputs do
      # if object has id we can destroy it
      if app_f.object.id
        app_f.input :_destroy, :as => :boolean, :label => "Supprimer l'utilisateur du projet"
      end
      app_f.input :user,      :include_blank => false, :label_method => :to_label
      app_f.input :role_name, :include_blank => false 
    end
  end
  f.buttons
end

I have the following associations :

Project

has_many :roles, :dependent => :destroy 
has_many :users, :through => :role

User

has_many :roles, :dependent => :destroy
has_many :projects, :through => :role 

Role

belongs_to :user
belongs_to :project
belongs_to :role_name

RoleName

has_many :roles

When I try to destroy user association through my form nothing happen, any idea to solve this ?
Or to add delete link to my show block ?

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

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

发布评论

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

评论(2

任谁 2025-01-09 19:57:08

尝试将 accepts_nested_attributes_for 添加到您的项目模型(并将 roles_attributes 添加到 attr_accessible):

class Project < ActiveRecord::Base
    has_many :roles, :dependent => :destroy 
    has_many :users, :through => :role
    accepts_nested_attributes_for :roles, :allow_destroy => true

    attr_accessible :roles_attributes, (+ all you had here before)
    ... 
end

Try to add accepts_nested_attributes_for to your Project model (and roles_attributes to attr_accessible):

class Project < ActiveRecord::Base
    has_many :roles, :dependent => :destroy 
    has_many :users, :through => :role
    accepts_nested_attributes_for :roles, :allow_destroy => true

    attr_accessible :roles_attributes, (+ all you had here before)
    ... 
end
无风消散 2025-01-09 19:57:08

allow_destroy: true 是此问题的根源。

allow_destroy: true is the root of this issue.

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