Rails表单不会更新所有字段

时间:2017-08-16 15:29:01

标签: ruby-on-rails ruby forms

我回来了另一个Rails问题。我有一个用于更新用户个人资料的表单,其中一部分有一个用于上传的个人资料图片。我也有一些裁剪,所以我在隐藏字段中传递一些裁剪信息(crop_x,crop_y,crop_w,crop_h)。

以下是我提交表单时日志中似乎发生的事情:

Started PATCH "/talent_profiles/5" for 127.0.0.1 at 2017-08-16 10:13:45 -0500
Processing by TalentProfilesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JO5VpUzKofGkNtf61LlfhzR3QbhI0GYuvyX5+Ev8lky272jkc7+ZpwnyRw2wZY4AIoWZlhZGvliGfSLvmXkKsw==", "talent_profile"=>{"profile_image"=>#<ActionDispatch::Http::UploadedFile:0x007fb277003198 @tempfile=#<Tempfile:/var/folders/4b/ft8tw3cj0fv77lg1b8mhn72r0000gn/T/RackMultipart20170816-23354-1wjo797.png>, @original_filename="Home.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"talent_profile[profile_image]\"; filename=\"Home.png\"\r\nContent-Type: image/png\r\n">, "crop_x"=>"622.0916920162501", "crop_y"=>"2175.4168294233164", "crop_w"=>"632.2108016602303", "crop_h"=>"632.2108016602303", "first_name"=>"Bob", "last_name"=>"Bobson"}, "commit"=>"Save Changes", "id"=>"5"}
  TalentProfile Load (0.2ms)  SELECT  "talent_profiles".* FROM "talent_profiles" WHERE "talent_profiles"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  SQL (0.5ms)  UPDATE "talent_profiles" SET "profile_image" = $1, "updated_at" = $2 WHERE "talent_profiles"."id" = $3  [["profile_image", "Home.png"], ["updated_at", "2017-08-16 15:13:46.061927"], ["id", 5]]
   (6.1ms)  COMMIT
Redirected to http://localhost:3000/talent_profiles/5
Completed 302 Found in 1064ms (ActiveRecord: 6.8ms)

正如您所看到的,它正在发送裁剪信息。但是,在运行@ profile.update_attributes(talent_profile_update_params)时,它永远不会更新talent_profile上的crop字段。我有:crop_x,:crop_y等添加到talent_profile_update_params中,如下所示:

def talent_profile_update_params
        params.require(:talent_profile).permit(:first_name, :last_name, :profile_image, :remove_profile_image, :crop_x, :crop_y, :crop_w, :crop_h)
end

如果这有帮助,这是我表单的起始行:

<%= form_for(@profile,:html=>{:id=>"update-profile",:multipart => true}) do |f| %>

你们是否有任何想法为什么这些字段没有被更新,即使它们在talent_profile_update_params中被允许?我有点疯狂......

更新:这是我的模特:

class TalentProfile < ApplicationRecord
    belongs_to :user
    mount_uploader :profile_image, ProfileImageUploader
    attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
    validates :profile_image, file_size: { less_than: 5.megabytes }
    after_update :crop_profile_image

    def crop_profile_image
        if crop_x.present?
            profile_image.recreate_versions!
        end
    end
end

0 个答案:

没有答案