【问题标题】:Rails_admin, remove nested model attributes from form while creating modelRails_admin,在创建模型时从表单中删除嵌套模型属性
【发布时间】:2015-04-08 12:34:23
【问题描述】:

我已经开始使用 Rails_Admin gem,它很棒。但是,有一个问题。我正在使用 Mongoid,并且我已经创建了我的模型:

class Client
   include Mongoid::Document

   field :email, type: String

   has_many :favourites, dependent: :destroy
end

class Service
   include Mongoid::Document

   field :name, type: String
   has_many :favourites, dependent: :destroy
end

class Favourite
   include Mongoid::Document

   belongs_to :client
   belongs_to :service
 end

我使用seed.rb 创建了一些数据。当我打开我的管理页面并尝试在Clients 属性字段之后创建新的Client 时,它会显示多选表单以添加Favourites 并显示Favourites 以及所有其他Clients

如何从创建表单中删除它?我已经读到我需要为我的所有类关联添加inverse_of,但它不会删除它们。

【问题讨论】:

    标签: ruby-on-rails ruby associations mongoid rails-admin


    【解决方案1】:

    在您的模型中:

    class Client
        include Mongoid::Document
        field :email, type: String
        has_many :favourites, dependent: :destroy
    
        rails_admin do
         edit do
           exclude_fields :favourites
         end
        end
    end
    

    来源:https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL#configuring-fields

    【讨论】:

    • 给我一个错误NoMethodError undefined method default for #<Client:0x0000000d569598>
    • 这个解决方案带来了新的问题。我在创建模型期间删除了嵌套表单。如果我想在我的模型的edit 页面中添加类似add Favourite for this Client 的内容,我需要做什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多