Active Admin, Rails 4 Nested DELETE Form, then INSERT has_one Model

When you edit a user who has a profile, Active admin DELETE the profile and re-create it, which makes deleting the avatar image using paperclip

How to get the active administrator to simply update the sub-form without deleting and re-creating.

here is my ActiveAdmin: User Code

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 

and this is what the console shows, note that she deleted the profile entry and created it again, which causes the paperclip pearl to delete the already updated image.

 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) 

any solution?

+4
source share
2 answers

I had the same problem. For nested attributes you need to use semantic_fields_for. Here is my solution:

 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.. ... 

Hope this can solve your problem.

0
source

Try resolving: id attribute for profile_attributes

 profile_attributes: [:id , ...] 

And try removing this monkeypatch:

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

Source: https://habr.com/ru/post/1494853/


All Articles