【问题标题】:ActiveAdmin form: multiple nested formsActiveAdmin 表单:多个嵌套表单
【发布时间】: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


【解决方案1】:

很遗憾,我还没有找到解决这个问题的正确方法,但它仍然是相关的(我不会结束这个问题,以防万一有一天它会回答)。

但我将行为建模为接近期望的行为。

因此,我在User 模型中添加了Avatar-interface 用于相应的对象。

  def avatar
    if self.profile && self.profile.image
      self.profile.image.img  
    else
      nil
    end
  end

  def avatar=(arg)
    unless self.profile.image
      self.profile.create_image(img:arg)
    else
      self.profile.image.update_attributes(img:arg)
    end
    self.profile.image
  end

  def avatar?
    if self.profile && self.profile.image
      !self.profile.image.img.nil?
    else
      nil
    end
  end

在 ActiveAdmin 中为用户添加了头像 permit_params。

 permit_params ..., :avatar

最后以添加文件字段的形式:

  form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs "User Details" do
      #Fields for User
    end
     f.inputs "profile", for:  [:profile, f.object.profile || f.object.build_profile] do |pf|
       #Fields for profile
    end
    f.inputs "Avatar" do
      f.input :avatar, as: :file
    end
    f.actions                                        
  end

值得注意的是,如果您在提交表单时观察到异常行为,为空值,那么您应该注意accepts_nested_attributes_for的参数,特别是update_onlyreject_if

我还在等待 ActiveAdmin 中多个嵌套表单的正常解决方案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2014-06-01
    • 1970-01-01
    相关资源
    最近更新 更多