Activeadmin : 选中 return 对象而不是名称
Activeadmin : Check boxes return the object and not the name
我的模型
# agency.rb
class Agency < ActiveRecord::Base
has_many :agency_prices
has_many :agency_cities
has_many :prices, through: :agency_prices
has_many :cities, through: :agency_cities
end
# price.rb
class Price < ActiveRecord::Base
has_many :agency_prices
has_many :agencies, through: :agency_prices
end
我的 activeadmin 代理文件
# admin/agency.rb
ActiveAdmin.register Agency do
permit_params :name,
city_ids: [],
price_ids: []
form do |f|
f.inputs "Agencies" do
f.input :name
f.input :cities, as: :check_boxes, checked: City.pluck(&:id)
f.input :prices, as: :check_boxes, checked: Price.pluck(&:price_range)
end
f.actions
end
end
我的问题
尽管对于城市,复选框显示我的 table "cities" 的 'name' 字段,但对于价格,复选框显示对象 #<Price:0x007f6f1dbe9b58>
而不是我的 table 价格中的 price_range 字段。
City 和 Price 的代码相同,在我的 tables / join tables 中一切似乎都是正确的。
你知道我可以找什么吗?
谢谢
在您的 Price
模型中,添加一个 name
方法,如下所示:
def name
self.price_range
end
我的模型
# agency.rb
class Agency < ActiveRecord::Base
has_many :agency_prices
has_many :agency_cities
has_many :prices, through: :agency_prices
has_many :cities, through: :agency_cities
end
# price.rb
class Price < ActiveRecord::Base
has_many :agency_prices
has_many :agencies, through: :agency_prices
end
我的 activeadmin 代理文件
# admin/agency.rb
ActiveAdmin.register Agency do
permit_params :name,
city_ids: [],
price_ids: []
form do |f|
f.inputs "Agencies" do
f.input :name
f.input :cities, as: :check_boxes, checked: City.pluck(&:id)
f.input :prices, as: :check_boxes, checked: Price.pluck(&:price_range)
end
f.actions
end
end
我的问题
尽管对于城市,复选框显示我的 table "cities" 的 'name' 字段,但对于价格,复选框显示对象 #<Price:0x007f6f1dbe9b58>
而不是我的 table 价格中的 price_range 字段。
City 和 Price 的代码相同,在我的 tables / join tables 中一切似乎都是正确的。
你知道我可以找什么吗?
谢谢
在您的 Price
模型中,添加一个 name
方法,如下所示:
def name
self.price_range
end