【问题标题】:Active Admin has_many selectable list of recordsActive Admin has_many 可选择的记录列表
【发布时间】:2014-09-19 09:39:59
【问题描述】:

我已经尝试了几天,我是 ROR 的新手和活跃的管理员。到目前为止,我已经能够为新记录添加和删除 has_many 关系。我正在使用strong_parameters 以及accept_nested_attributes。我想要的

  • 还可以为现有记录添加和删除关系。

理想情况下,应该有一个自动完成框,允许搜索和选择此特定模型的现有含义。

我的模型是

  • 意义
  • 字义

我只希望能够为单词附加已有的含义吗?

      class Word < ActiveRecord::Base    
         belongs_to :language    
         has_many :word_meanings
         has_many :meanings ,through: :word_meanings

form do |f|

f.semantic_errors *f.object.errors.keys
f.inputs do
  f.input :language
  f.input :word
  f.input :wordInScript
  f.input :pronunciation, :required => false, :as => :file
end

f.inputs do
  f.has_many :meanings, heading: 'Meanings', allow_destroy: true, new_record: true do |a|
    a.input :meaning
    a.input :language
  end
end

f.actions
end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 activeadmin has-many-through


    【解决方案1】:

    可以确定select的集合:

    a.input :meaning, :as => :select, :collection => {#your collection in Hash, Array or ActiveRecord relation}.map{|key, value| [value, key] }
    

    ActiveAdmin 使用 Formtastic: https://github.com/justinfrench/formtastic#usage

    【讨论】:

    • 好的,我应该删除“a.input :language”然后
    • 但它显示了含义的 ID,我的实际数据库中有 50000 行。我该如何管理?
    • 不工作!我还能保持创造新意义的能力吗?
    • your_collectoin.map{|键,值| [value, key] } -> 在这种情况下不会出现 id
    • 当你有这么多的记录时,你可以用 ajax 和 select2 为例
    猜你喜欢
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多