每当您编辑具有has_one配置文件的用户时,事件管理员都会删除该配置文件并重新创建,从而使回形针图像被回形针删除

我如何才能使事件管理员仅更新嵌套表单而不删除并重新创建它。

这是我的ActiveAdmin:用户代码

ActiveAdmin.register User do
  menu :priority => 2

  ## Controller
  controller do

    def update
      if params[:user][:password].blank?
        params[:user].delete("password")
        params[:user].delete("password_confirmation")
      end

      if params[:user][:profile_attributes][:avatar].blank?
        params[:user][:profile_attributes].delete("avatar")
      end

      super
    end

    def permitted_params
      params.permit user: [:username, :email, :password, :password_confirmation, :approved, role_ids: [], profile_attributes: [:first_name, :last_name, :date_of_birth, :avatar]]
    end

  end

  #Scopes
  scope :all, default: true
  scope :admins
  scope :editors
  scope :members

  ## Filters
  filter :username
  filter :email

  ## Index
  index do
    column :id
    column :first_name do |user|
      user.profile.first_name unless user.profile.nil?
    end
    column :last_name do |user|
      user.profile.last_name unless user.profile.nil?
    end
    column :username
    column :email
    column :role, :sortable => false
    default_actions
  end

  ## Form
  form :html => {:multipart => true} do |f|

    f.inputs "User Account Details" do
      f.input :username
      f.input :email
      f.input :roles
      f.input :approved
      f.input :password, hint: "leave it blank if you don't want to change it"
      f.input :password_confirmation
    end

    f.inputs "Profile Details",  for:  [:profile, f.object.profile || f.object.build_profile] do |profile_form|
        profile_form.input :first_name
        profile_form.input :last_name
        profile_form.input :date_of_birth, as: :date_select, start_year: Date.today.year-80, end_year: Date.today.year-13

        profile_form.input :avatar, :as => :file, :hint => profile_form.template.image_tag(profile_form.object.avatar.url(:small))
    end

    f.actions
  end

end

这就是控制台显示的内容,请注意,它删除了配置文件记录,然后再次重新创建它,这使回形针gem删除了已更新的图像。
Started PATCH "/admin/users/1" for 127.0.0.1 at 2013-08-02 01:29:09 +0200
Processing by Admin::UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"m9tBWsM+Jq0CWmSu2kCxCMn8xV3pgYQ0iFz5K9X5EjY=", "user"=>{"username"=>"admin", "email"=>"[email protected]", "role_ids"=>["", "", "1", "2"], "approved"=>"1", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "profile_attributes"=>{"first_name"=>"Moe", "last_name"=>"Hassan", "date_of_birth(1i)"=>"1935", "date_of_birth(2i)"=>"4", "date_of_birth(3i)"=>"4", "id"=>"58"}}, "commit"=>"Update User", "id"=>"1"}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
   (0.3ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 1]]
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", "1"]]
Unpermitted parameters: id
Unpermitted parameters: utf8, _method, authenticity_token, commit, id
  Role Load (0.4ms)  SELECT "roles".* FROM "roles" WHERE "roles"."id" IN (1, 2)
  Role Load (0.3ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1  [["user_id", 1]]
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
  Profile Load (0.2ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1  [["user_id", 1]]
   (0.1ms)  BEGIN
[AWS S3 200 1.692562 0 retries] head_object(:bucket_name=>"assets.goldpricenetwork.dev",:key=>"public/avatars/58/original-901301_3903423203601_1063661495_o.jpg")

[AWS S3 200 0.305978 0 retries] head_object(:bucket_name=>"assets.goldpricenetwork.dev",:key=>"public/avatars/58/small-901301_3903423203601_1063661495_o.jpg")

  SQL (0.3ms)  DELETE FROM "profiles" WHERE "profiles"."id" = $1  [["id", 58]]
[paperclip] deleting public/avatars/58/original-901301_3903423203601_1063661495_o.jpg
[AWS S3 204 0.261193 0 retries] delete_object(:bucket_name=>"assets.goldpricenetwork.dev",:key=>"public/avatars/58/original-901301_3903423203601_1063661495_o.jpg")

[paperclip] deleting public/avatars/58/small-901301_3903423203601_1063661495_o.jpg
[AWS S3 204 0.255899 0 retries] delete_object(:bucket_name=>"assets.goldpricenetwork.dev",:key=>"public/avatars/58/small-901301_3903423203601_1063661495_o.jpg")

   (2.2ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.5ms)  INSERT INTO "profiles" ("created_at", "date_of_birth", "first_name", "last_name", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["created_at", Thu, 01 Aug 2013 23:29:12 UTC +00:00], ["date_of_birth", Thu, 04 Apr 1935], ["first_name", "Moe"], ["last_name", "Hassan"], ["updated_at", Thu, 01 Aug 2013 23:29:12 UTC +00:00], ["user_id", 1]]
   (0.3ms)  COMMIT
Redirected to http://localhost:3000/admin/users/1
Completed 302 Found in 2552ms (ActiveRecord: 5.8ms)

有什么办法吗?

最佳答案

尝试允许profile属性的:id属性

profile_attributes: [:id , ...]

并尝试删除此Monkeypatch:
  if params[:user][:profile_attributes][:avatar].blank?
    params[:user][:profile_attributes].delete("avatar")
  end

关于forms - 事件管理员,Rails 4嵌套表格DELETE然后INSERT has_one模型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18006367/

10-10 04:40