Active Admin表单仅显示“#<keyword:0x10c858858>”

时间:2018-03-03 08:21:20

标签: ruby-on-rails ruby-on-rails-3 activerecord rails-activerecord activeadmin

我正在Active Admin中处理HABTM关系。我有两个模型:magazine.rbkeyword.rb,两者都通过联接表magazines_keyword.rb相关联。我希望能够从杂志资源页面中选择Active Admin关键字。

AA资源文件magazines.rb如下:

ActiveAdmin.register Magazine do
menu :priority => 1

form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs "Magazine Details" do
        f.input :title_id, :label => 'Title', :as => :select, :collection => Keyword.all.map{ |u| ["#{u.word.capitalize}", u.id] }
        f.input :keywords, :as => :check_boxes, :input_html => { :multiple => true } do
            f.has_many :keywords do |s|
                s.input :keyword, :collection => Keyword.all.map { |kw| ["#{kw.word.capitalize}", kw.id] }
            end
        end
    end
    f.actions
end

show do
    panel "Magazine Details" do
        attributes_table_for magazine do
            row "Keywords", :keyword do |m|
                m.keywords.map { |d| d.word }.join(", ").html_safe
            end
        end
    end
    active_admin_comments
end

节目部分有效。使用此设置,我还可以输入新杂志并编写连接表,但表单不显示关键字,而只显示#<Keyword:0x11adf6608>。这是什么?该字段的Active Records地址?

如何让表单显示每条记录的正确数据条目?

当我使用时:

f.input :keywords, :as => :check_boxes, :input_html => { :multiple => true }, :collection => Keyword.all.map { |kw| ["#{kw.word.capitalize}", kw.id] }

显示关键字,但联接表似乎无法正常工作,因为我无法阅读,编辑或编写新的或现有的条目。

如何让这部分工作?

更新

当我用

创建一个新杂志条目时

f.input :keywords, :as => :check_boxes

Started GET "/admin/magazines/new" for 127.0.0.1 at Sat Mar 03 14:14:55 +0100 2018
Processing by Admin::Magazines Controller#new as HTML
  [1m[35mAdminUser Load (0.5ms)[0m  SELECT admin_users.* FROM admin_users WHERE admin_users.id = 1 LIMIT 1
  [1m[36mKeyword Load (1.5ms)[0m  [1mSELECT keywords.* FROM keywords [0m
  [1m[35mCACHE (0.0ms)[0m  SELECT keywords.* FROM keywords 
  Rendered /Users/xxxxx/.rvm/gems/xxxxx/gems/activeadmin-0.6.0/app/views/active_admin/resource/new.html.arb (378.3ms)
Completed 200 OK in 387ms (Views: 380.3ms | ActiveRecord: 1.9ms)


Started POST "/admin/magazines" for 127.0.0.1 at Sat Mar 03 14:15:09 +0100 2018
Processing by Admin::Magazines Controller#create as HTML
  Parameters: {"authenticity_token"=>"zzzzz=", "commit"=>"Create Magazine", "utf8"=>"✓", "Magazine"=>{"number"=>"10", "keyword_ids"=>["", "1", "2"], "description"=>"3", "colour"=>"4", "title_id"=>"1", "cover_alt"=>"5", "short"=>"2"}}
  [1m[36mAdminUser Load (0.5ms)[0m  [1mSELECT admin_users.* FROM admin_users WHERE admin_users.id = 1 LIMIT 1[0m
  [1m[35mKeyword Load (0.4ms)[0m  SELECT keywords.* FROM keywords WHERE keywords.id IN (1, 2)
  [1m[36mSQL (0.1ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (1.2ms)[0m  INSERT INTO magazines (colour, cover_alt, created_at, description, number, short, title_id, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["colour", "4"], ["cover_alt", "5"], ["created_at", Sat, 03 Mar 2018 13:15:09 UTC +00:00], ["description", "3"], ["number", 10], ["short", "2"], ["title_id", 1], ["updated_at", Sat, 03 Mar 2018 13:15:09 UTC +00:00]]
  [1m[36m (0.3ms)[0m  [1mINSERT INTO magazines_keywords (magazine_id, keyword_id) VALUES (13, 1)[0m
  [1m[35m (0.2ms)[0m  INSERT INTO magazines_keywords (magazine_id, keyword_id) VALUES (13, 2)
  [1m[36m (6.1ms)[0m  [1mCOMMIT[0m
Redirected to http://localhost:3000/admin/safts/13
Completed 302 Found in 29ms (ActiveRecord: 0.0ms)

并且:

f.input :keywords, :as => :check_boxes, :input_html => { :multiple => true } do
    f.has_many :keywords do |s|
        s.input :keyword, :collection => Keyword.all.map { |kw| ["#{kw.word.capitalize}", kw.id] }
    end
end

我明白了:

Started GET "/admin/magazines/new" for 127.0.0.1 at Sat Mar 03 14:21:41 +0100 2018
Processing by Admin::MagazinesController#new as HTML
  [1m[36mAdminUser Load (0.4ms)[0m  [1mSELECT 'admin_users'.* FROM 'admin_users' WHERE 'admin_users'.'id' = 1 LIMIT 1[0m
  [1m[35mKeyword Load (1.4ms)[0m  SELECT 'keywords'.* FROM 'keywords' 
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT 'keywords'.* FROM 'keywords' [0m
  Rendered /Users/xxxxx/.rvm/gems/xxxxx/gems/activeadmin-0.6.0/app/views/active_admin/resource/new.html.arb (300.6ms)
Completed 200 OK in 390ms (Views: 302.4ms | ActiveRecord: 1.8ms)


Started POST "/admin/magazines" for 127.0.0.1 at Sat Mar 03 14:21:54 +0100 2018
Processing by Admin::MagazinesController#create as HTML
  Parameters: {"authenticity_token"=>"zzzzz=", "commit"=>"Create Magazine", "utf8"=>"✓", "magazine"=>{"number"=>"11", "keyword_ids"=>["", "1", "2"], "description"=>"4", "colour"=>"5", "title_id"=>"2", "cover_alt"=>"6", "short"=>"3"}}
  [1m[35mAdminUser Load (0.4ms)[0m  SELECT 'admin_users'.* FROM 'admin_users' WHERE 'admin_users'.'id' = 1 LIMIT 1
  [1m[36mKeyword Load (0.3ms)[0m  [1mSELECT 'keywords'.* FROM 'keywords' WHERE 'keywords'.'id' IN (1, 2)[0m
  [1m[35mSQL (0.1ms)[0m  BEGIN
  [1m[36mSQL (0.9ms)[0m  [1mINSERT INTO 'magazines' ('colour', 'cover_alt', 'created_at', 'description', 'number', 'short', 'title_id', 'updated_at') VALUES (?, ?, ?, ?, ?, ?, ?, ?)[0m  [["colour", "5"], ["cover_alt", "6"], ["created_at", Sat, 03 Mar 2018 13:21:54 UTC +00:00], ["description", "4"], ["number", 11], ["short", "3"], ["title_id", 2], ["updated_at", Sat, 03 Mar 2018 13:21:54 UTC +00:00]]
  [1m[35m (0.3ms)[0m  INSERT INTO 'magazines_keywords' ('magazine_id', 'keyword_id') VALUES (14, 1)
  [1m[36m (0.2ms)[0m  [1mINSERT INTO 'magazines_keywords' ('magazine_id', 'keyword_id') VALUES (14, 2)[0m
  [1m[35m (12.1ms)[0m  COMMIT
Redirected to http://localhost:3000/admin/magazines/14
Completed 302 Found in 114ms (ActiveRecord: 0.0ms)

1 个答案:

答案 0 :(得分:2)

我认为您需要使用:

f.input :keywords, as: :check_boxes, member_label: :word

f.input :keywords, as: :check_boxes, member_label: Proc.new { |kw| kw.word.capitalize }