【发布时间】:2015-03-09 11:24:28
【问题描述】:
我有一个结构User-> Profile-> Image,想在ActiveAdmin 中使用一种形式进行编辑和录制。
我在模型中使用accepts_nested_attributes_for:
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy;
accepts_nested_attributes_for :profile, allow_destroy: true
end
class Profile < ActiveRecord::Base
belongs_to :image, dependent: :destroy;
accepts_nested_attributes_for :image,:reject_if => proc { |attributes| !attributes['img'].present? }, :allow_destroy => true
end
class Image < ActiveRecord::Base
has_attached_file :img
validates_attachment_content_type :img, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
还有ActiveAdmin.register User中的permit_params:
permit_params do
permitted=[:id,:login, :email, :admin, :password, :password_confirmation, :ip_address];
permitted.append(profile_attributes:[:name,:second_name,:middle_name,:img,:mobile_phone,:country, :city,:region, image_attributes:[:img]]);
permitted
end
最后,代码本身就形成了
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "User Details" do
f.input :login
f.input :email
f.input :password
f.input :password_confirmation
end
f.inputs "Profile", for: [:profile, f.object.profile || f.object.build_profile] do |pf|
pf.input :name
pf.input :second_name
pf.input :middle_name
pf.input :mobile_phone, :as => :phone
pf.input :country, selected: "RU"
pf.input :city
pf.input :region
pf.inputs "Avatar", for:[:image, pf.object.image || pf.object.build_image] do |fpf|
fpf.input :img, :as => :file
end
end
f.inputs "User Perference" do
f.input :admin, type: :boolean
end
f.actions
end
不幸的是,此代码不起作用:该表单可以正确显示Profile 并且可以正常工作,但该表单对Image 不可见。我该如何解决这个问题?
【问题讨论】:
-
为什么 Profile
belongs_to :image而不是相反? -
将
FK存储在profiles表中,而不是images。图像模型稍后将与不同模型一起使用,应该不是由于其他。 -
我已经尝试过类似的方法,但没有成功。 :( 我建议你将 Image 模型绑定到 User。
-
我在
update-action 中找到了SimpleForm,但ActiveAdmin仍然继续忽略多个嵌套表单。 -
也许为时已晚,但您可以在这里找到解决方案:stackoverflow.com/questions/32563922/…
标签: ruby-on-rails forms ruby-on-rails-4 activeadmin nested-forms