Active Admin表单仅显示“# ”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Active Admin表单仅显示“# ”相关的知识,希望对你有一定的参考价值。
我正在Active Admin中处理HABTM关系。我有两个模型:magazine.rb
和keyword.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] }
显示关键字,但连接表似乎不起作用,因为我无法读取,编辑或写入新的或现有的条目。
我该如何使这部分工作?
UPDATE
当我创建一个新的杂志条目时
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)
答案
我想你需要使用:
f.input :keywords, as: :check_boxes, member_label: :word
要么
f.input :keywords, as: :check_boxes, member_label: Proc.new { |kw| kw.word.capitalize }
以上是关于Active Admin表单仅显示“# ”的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 8 Autocomplete Broken - 仅显示有限的用户代码片段 - 知道为啥吗?
如何使子表单行项目在 zoho creator 片段页面的 HTML 表格中仅出现一次?