質問内容・実現したいこと
・編集画面から写真を変更したい。
現状発生している問題・エラーメッセージ
・編集することができない。
どの処理までうまく動いているのか
・avater
以外、送られていることが確認できました。
ruby
1From: /Users/mark_naito/workspace/runteq/RUNTEQ_basic_level/948_mmmaaarrrkkk000_runteq_learning_basic/app/controllers/profiles_controller.rb @ line 9 : 2 3 4: def show; end 4 5: 5 6: def edit; end 6 7: 7 8: def update 8 => 9: binding.irb 9 10: if @user.update(user_params) 10 11: redirect_to profile_path, success: t('.success') 11 12: else 12 13: flash.now[:danger] = t('fail') 13 14: render :edit 14 15irb(#<ProfilesController:0x00007fda868aaf80>):001:0> @user 16=> #<User id: 2, email: "1111@example.com", crypted_password: "$2a$10$xJt39WxBGYL/5Q.CbcjkQ.YMB09T.mk2Xv3O3JgqJDk...", salt: "4UeKEoKCCWknxJpLphD5", last_name: "1111", first_name: "1111", created_at: "2021-07-13 07:11:14", updated_at: "2021-07-13 07:11:14", avater: nil> 17:002:0> user_params 18=> <ActionController::Parameters {"email"=>"1111@example.com", "avater"=>#<ActionDispatch::Http::UploadedFile:0x00007fa6b1d568a8 @tempfile=#<Tempfile:/var/folders/8p/r7ndt03s6610f5cscd0hk6rw0000gn/T/RackMultipart20210715-94120-oac1ye.jpg>, @original_filename="DSCF9807.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avater]\"; filename=\"DSCF9807.jpg\"\r\nContent-Type: image/jpeg\r\n">} permitted: true>
ruby
1From: /Users/mark_naito/workspace/runteq/RUNTEQ_basic_level/948_mmmaaarrrkkk000_runteq_learning_basic/app/views/profiles/edit.html.erb @ line 7 : 2 3 2: <div class='container'> 4 3: <div class='row'> 5 4: <div class='col-md-10 offset-md-1'> 6 5: <h1><%= t('.title') %></h1> 7 6: <%= form_with model: current_user, url: profile_path, method: :patch, local: true do |f| %> 8 => 7: <% binding.irb %> 9 8: <%= render 'shared/errors_message', object: f.object %> 10 9: <div class='form-group'> 11 10: <%= f.label :email %> 12 11: <%= f.text_field :email, class: 'form-control' %> 13 12: </div> 14 15irb(#<#<Class:0x00007fad3094ff18>:0x00007fad27c1aa08>):001:0> current_user 16=> #<User id: 2, email: "1111@example.com", crypted_password: "$2a$10$xJt39WxBGYL/5Q.CbcjkQ.YMB09T.mk2Xv3O3JgqJDk...", salt: "4UeKEoKCCWknxJpLphD5", last_name: "1111", first_name: "1111", created_at: "2021-07-13 07:11:14", updated_at: "2021-07-13 07:11:14", avater: nil>
該当のソースコード
profile_control.rb
ruby
1class ProfilesController < ApplicationController 2 before_action :set_user, only: %i[edit show update] 3 4 def show; end 5 6 def edit; end 7 8 def update 9 if @user.update(user_params) 10 redirect_to profile_path, success: t('.success') 11 else 12 flash.now[:danger] = t('fail') 13 render :edit 14 end 15 end 16 17 private 18 19 def set_user 20 @user = User.find(current_user.id) 21 end 22 23 def user_params 24 params.require(:user).permit(:email, :last_name, :first_name, :avater) 25 end 26end
user.rb
ruby
1mount_uploader :avater, ImageUploader
profile/edit.html.erb
ruby
1<% content_for(:title, t('.title')) %> 2<div class='container'> 3 <div class='row'> 4 <div class='col-md-10 offset-md-1'> 5 <h1><%= t('.title') %></h1> 6 <%= form_with model: current_user, url: profile_path, method: :patch, local: true do |f| %> 7 <%= render 'shared/errors_message', object: f.object %> 8 <div class='form-group'> 9 <%= f.label :email %> 10 <%= f.text_field :email, class: 'form-control' %> 11 </div> 12 <div class='form-group'> 13 <%= f.label :last_name %> 14 <%= f.text_field :email, class: 'form-control' %> 15 </div> 16 <div class='form-group'> 17 <%= f.label :first_name %> 18 <%= f.text_field :email, class: 'form-control' %> 19 </div> 20 <div class='form-group'> 21 <%= f.label :avater %> 22 <%= f.file_field :avater, 23 class: 'form-control mb-3', 24 accept: 'image/*', 25 onchange: 'previewFileWithId(preview)' %> 26 <div class='mt-3 mb-3'> 27 <%= image_tag current_user.avater.url, id: 'preview', class: 'rounded-circle', width: '100', height: '100' %> 28 </div> 29 </div> 30 <%= f.submit class: 'btn btn-primary' %> 31 <% end %> 32 </div> 33 </div> 34</div>
avater_upllader.rb
ruby
1class AvaterUploader < CarrierWave::Uploader::Base 2 # Include RMagick or MiniMagick support: 3 # include CarrierWave::RMagick 4 # include CarrierWave::MiniMagick 5 6 # Choose what kind of storage to use for this uploader: 7 storage :file 8 # storage :fog 9 10 # Override the directory where uploaded files will be stored. 11 # This is a sensible default for uploaders that are meant to be mounted: 12 def store_dir 13 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 14 end 15 16 # Provide a default URL as a default if there hasn't been a file uploaded: 17 def default_url 18 'sample.jpg' 19 end 20 21 # Process files as they are uploaded: 22 # process scale: [200, 300] 23 # 24 # def scale(width, height) 25 # # do something 26 # end 27 28 # Create different versions of your uploaded files: 29 # version :thumb do 30 # process resize_to_fit: [50, 50] 31 # end 32 33 # Add an allowlist of extensions which are allowed to be uploaded. 34 # For images you might use something like this: 35 def extension_allowlist 36 %w(jpg jpeg gif png) 37 end 38 39 # Override the filename of the uploaded files: 40 # Avoid using model.id or version_name here, see uploader/store.rb for details. 41 # def filename 42 # "something.jpg" if original_filename 43 # end 44end
サーバーログ
ruby
1Started PATCH "/profile" for ::1 at 2021-07-15 13:59:11 +0900 2Processing by ProfilesController#update as HTML 3 Parameters: {"utf8"=>"✓", "authenticity_token"=>"Iji82Az0iGs4fQOhfcF59IhP/Waw+fBAfwGyJ1VtmSS8kKiKeuzV5VKKbwB0rX/YAa0tTJANV7+AVJjgzp748w==", "user"=>{"email"=>"1111@example.com", "avater"=>#<ActionDispatch::Http::UploadedFile:0x00007fad30bb0c58 @tempfile=#<Tempfile:/var/folders/8p/r7ndt03s6610f5cscd0hk6rw0000gn/T/RackMultipart20210715-94262-rt7ou9.jpg>, @original_filename="DSCF9824.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avater]\"; filename=\"DSCF9824.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"更新する"} 4 User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] 5 ↳ vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 6 User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] 7 ↳ app/controllers/profiles_controller.rb:21 8 (0.1ms) begin transaction 9 ↳ app/controllers/profiles_controller.rb:9 10 User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? AND "users"."id" != ? LIMIT ? [["email", "1111@example.com"], ["id", 2], ["LIMIT", 1]] 11 ↳ app/controllers/profiles_controller.rb:9 12 (0.1ms) rollback transaction 13 ↳ app/controllers/profiles_controller.rb:9 14 Rendering profiles/edit.html.erb within layouts/application 15 Rendered profiles/edit.html.erb within layouts/application (10.4ms) 16 Rendered shared/_flash_message.html.erb (0.4ms) 17 Rendered shared/_header.html.erb (3.4ms) 18 Rendered shared/_footer.html.erb (0.5ms) 19Completed 200 OK in 183ms (Views: 134.0ms | ActiveRecord: 0.6ms)
エラーから考えられる原因
・updateアクションで保存ができていないからでしょうか。
サーバログで変更が取り消されている箇所を確認
bash
1rollback transaction 2 ↳ app/controllers/profiles_controller.rb:9
updateに感嘆符をつけエラーの原因を特定する事ができました。
ruby
1def update 2 if @user.update!(user_params) 3. 4.
user.rb で定義したバリデーション引っかかってしまい更新できないということがわかりました。
=>#<ActionDispatch::Http::UploadedFile:0x00007feaab554a68 @tempfile=#<Tempfile:/var/folders/8p/r7ndt03s6610f5cscd0hk6rw0000gn/T/RackMultipart20210715-94737-1yepbxn.jpg>, @original_filename="DSCF9843.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avater]\"; filename=\"DSCF9843.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"更新する"} User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] ↳ vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 (0.1ms) begin transaction ↳ app/controllers/profiles_controller.rb:9 User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? AND "users"."id" != ? LIMIT ? [["email", "1111@example.com"], ["id", 2], ["LIMIT", 1]] ↳ app/controllers/profiles_controller.rb:9 (0.1ms) rollback transaction ↳ app/controllers/profiles_controller.rb:9 Completed 422 Unprocessable Entity in 106ms (ActiveRecord: 0.8ms) ActiveRecord::RecordInvalid (バリデーションに失敗しました: パスワードは3文字以上で入力してください, パスワード確認を入力してください):
この先どう手をつけたらいいか分からずにいます。。
ご教授いただきたいです。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー