【问题标题】:ActiveAdmin AutocompleteActiveAdmin 自动完成
【发布时间】:2016-03-04 07:08:54
【问题描述】:

我有一个名为 LetterResponse

的模型

belongs_to :user

LetterResponse 有以下字段(只是缩短了我的模型)

:id
:title
:user_id

当管理员创建新的 letter_response 时,他将填写标题,当他输入用户名时,它应该自动填写

我有 10 万用户,所以不能这样做

f.input :response_user_id, :as => :select, :collection => 
  User.all.collect {|user| [user.first_name, user.user_id] }, label: "Parent" 

即使 choozen-rails gem 也会执行相同的预加载数据。

所以尝试通过以下方式使用 activeadmin-addons ajax-search

https://github.com/platanus/activeadmin_addons/blob/master/docs/select2_search.md

f.input :response_user_id, as: :search_select, url: admin_users_path,
          fields: [:user_last_name], display_name: 'name', 
                minimum_input_length: 2

但我得到一个错误:

未定义的方法 response_user

如何进行?

【问题讨论】:

    标签: ruby-on-rails-4 activeadmin


    【解决方案1】:

    您的输入字段不应该是 :user_id 而不是 :response_user_id,(因为您提到您的 LetterResponse 具有belongs_to :user 关系(而不是belongs_to :response_user。

    所以,它应该看起来像

    f.input :user_id, as: :search_select, url: admin_users_path, fields: [:user_last_name], display_name: 'name', minimum_input_length: 2
    

    【讨论】:

      猜你喜欢
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2012-06-12
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多