5

当您编辑拥有一个配置文件的用户时,活动管理员删除配置文件并重新创建它,这使得头像图像被回形针删除

我怎样才能让活动管理员只更新嵌套表单而不删除并重新创建它。

这是我的 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"=>"admin@example.com", "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)

任何解决方案?

4

2 回答 2

1

尝试允许 profile_attributes 的 :id 属性

profile_attributes: [:id , ...]

并尝试删除此猴子补丁:

  if params[:user][:profile_attributes][:avatar].blank?
    params[:user][:profile_attributes].delete("avatar")
  end
于 2014-11-18T10:26:52.020 回答
0

我有同样的问题。您需要将语义字段用于嵌套属性。这是我的解决方案:

f.inputs "User Fundamentals" do
...
end
if params[:action] == 'edit'
  f.inputs "User Profile" do
      f.semantic_fields_for :profile do |profile_form|
        profile_form.inputs :first_name,:last_name, etc..
      ...
elsif params[:action] == 'new'
  f.inputs :name => "User Profile", :for => [Profile.new] do |profile_form|
        profile_form.inputs :first_name,:last_name, etc..
      ...

希望这可以解决您的问题。

于 2013-08-21T12:35:37.897 回答