題名の通り、viewの入力値がDBに保存されていない状態を解決したいです。
もともと、うまくいかなかった理由をデバックのログで確認した状態から、自分としては直したと感じている部分まで記述します。
###ソースコード(Github)
Githubリンク
サインインにはdeviseを使用しています。
###デバックで得られた結果
terminal
1Started POST "/users" for ::1 at 2019-07-27 15:04:12 +0900 2Processing by Devise::RegistrationsController#create as HTML 3 Parameters: {"utf8"=>"✓", "authenticity_token"=>"cHSz4U5k+wLShPDyQZW06+4s32RrUKgco1NSg21EBBio/+29cvZrxY4WpykTma7MYHtQHiDcZjJ3I48n+mpRbQ==", "user"=>{"name"=>"hiroki", "email"=>"tt@tt.com", "introduce"=>"hajimemasite", "self_image"=>#<ActionDispatch::Http::UploadedFile:0x007f9dda2e9f80 @tempfile=#<Tempfile:/var/folders/9j/zkxb427x5zd1xjty84shsmk80000gn/T/RackMultipart20190727-29392-18d4pst.JPG>, @original_filename="IMG_2349.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[self_image]\"; filename=\"IMG_2349.JPG\"\r\nContent-Type: image/jpeg\r\n">, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 4Unpermitted parameters: :introduce, :image
parameterで、introduceとimageが許可されていないのが原因らしいので、それを解決すればいい!と考えた。
###viewを確認
deviseを使用しているので、他の入力欄と同様にコピーして該当箇所(introduce、image,)を追加作成。
app/views/devise/registrations/new.html.erb
rails5
1<h2>Sign up</h2> 2 3<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 4 <%= render "devise/shared/error_messages", resource: resource %> 5 6 <div class="field"> 7 <%= f.label :name %><br /> 8 <%= f.text_field :name, autofocus: true %> 9 </div> 10 11 <div class="field"> 12 <%= f.label :email %><br /> 13 <%= f.email_field :email, autofocus: true, autocomplete: "email" %> 14 </div> 15 16 <!-- ここで紹介文を書かせる --> 17 <div class="field"> 18 <%= f.label :introduce %><br /> 19 <%= f.text_area :introduce, autocomplete: "new-introduce" %> 20 </div> 21 22 <!-- ここでimage画像をアップロードさせる --> 23 <h5>backgroud画像を選択してください</h5> 24 <div class="field"> 25 <%= f.label :image %><br /> 26 <%= f.file_field :image, autocomplete: "new-image" %> 27 </div> 28 29 30 <div class="field"> 31 <%= f.label :password %> 32 <% if @minimum_password_length %> 33 <em>(<%= @minimum_password_length %> characters minimum)</em> 34 <% end %><br /> 35 <%= f.password_field :password, autocomplete: "new-password" %> 36 </div> 37 38 39 <div class="field"> 40 <%= f.label :password_confirmation %><br /> 41 <%= f.password_field :password_confirmation, autocomplete: "new-password" %> 42 </div> 43 44 45 46 <div class="actions"> 47 <%= f.submit "Sign up" %> 48 </div> 49<% end %>
###コントローラ(該当箇所)を確認
app/controller/users/regstrations_controller.rb
rails5
1class Users::RegistrationsController < Devise::RegistrationsController 2 before_action :configure_sign_up_params, only: [:create] 3 before_action :configure_account_update_params, only: [:update] 4 5------------------------------------- 6↑省略 7 8 # POST /resource 9 def create 10 super 11 logger.debug("コントローラーが終了")→上記の「デバックで得られた結果」はこの部分に相当する 12 end 13 14↓省略 15------------------------------------- 16 protected 17 ↓ストロングパラメータ該当箇所 18 # If you have extra params to permit, append them to the sanitizer. 19 def configure_sign_up_params 20 #devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) 21 #↑修正前 22 devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :introduce, :image]) 23 #↑修正後 24 end 25 26 27 # If you have extra params to permit, append them to the sanitizer. 28 def configure_account_update_params 29 devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 30 end 31 32 # The path used after sign up. 33 def after_sign_up_path_for(resource) 34 super(resource) 35 end 36 37 # The path used after sign up for inactive accounts. 38 def after_inactive_sign_up_path_for(resource) 39 super(resource) 40 end 41end
デバック内容ではストロングパラメータに関して、unpermittedだったので、その部分を上記の通り修正、記述した。
###修正後にもう一度、サインインをやり直してみると・・
1、railsコンソールから、Userデータについて確認すると、introduceとimage欄がnilになっている。
terminal
1=> #<ActiveRecord::Relation [#<User id: 1, email: "tt@tt.com", created_at: "2019-07-27 06:29:18", updated_at: "2019-07-27 06:29:18", name: "ひろき", image: nil, introduce: nil>]>
2、デバックはどうなったか?
エラーそのものに変化がない。
terminal
1Started POST "/users" for ::1 at 2019-07-27 15:29:18 +0900 2Processing by Devise::RegistrationsController#create as HTML 3 Parameters: {"utf8"=>"✓", "authenticity_token"=>"IkVgnhyy0AxzsQRDfow+fFsGLNMMwxv39riaUa816F0t1saRTYEAWuRylwQMOY2q1HPxOqv2Od6t0+XPE4z17Q==", "user"=>{"name"=>"ひろき", "email"=>"tt@tt.com", "introduce"=>"初めまして", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fc8ce439038 @tempfile=#<Tempfile:/var/folders/9j/zkxb427x5zd1xjty84shsmk80000gn/T/RackMultipart20190727-30076-ff2jes.JPG>, @original_filename="IMG_2352.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[image]\"; filename=\"IMG_2352.JPG\"\r\nContent-Type: image/jpeg\r\n">, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 4Unpermitted parameters: :introduce, :image
###質問のまとめ
parameterのunpermitted部分を修正すればうまくいくのでは?と仮説を立てて検証したが、うまくいかなかった。
おそらくこの部分を勘違いしているのか、または別に問題がある可能性が高いが、学習時間に限りがあるため、調べながら質問させていただきます。
よろしくお願いいたします。
###参照した情報(一部抜粋=質問内容に似ていると感じた)
https://teratail.com/questions/182766
→このエラー内容とは違うと感じます
###@8zcaさん
ご回答の内容は、そもそもregstrations_controllerのcreateアクションが呼ばれていないということでした。
この部分に関してはこの後すぐ調べたいと思います。
terminal
1Started POST "/users" for ::1 at 2019-07-28 18:44:48 +0900 2Processing by Devise::RegistrationsController#create as HTML 3 Parameters: {"utf8"=>"✓", "authenticity_token"=>"FxJK+o8PEhIInLDmGSrvCPf7w7gjp1TOt1DyC88npe+WJwXTacLlduhw+gsDbjpV+sRV2GNEVDBV94vkfcbj8Q==", "user"=>{"name"=>"あああ", "email"=>"test@test.com", "introduce"=>"初めまして", "image"=>#<ActionDispatch::Http::UploadedFile:0x007ffc9c9ee3a0 @tempfile=#<Tempfile:/var/folders/9j/zkxb427x5zd1xjty84shsmk80000gn/T/RackMultipart20190728-997-9hun8x.JPG>, @original_filename="IMG_2349.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[image]\"; filename=\"IMG_2349.JPG\"\r\nContent-Type: image/jpeg\r\n">, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 4Unpermitted parameters: :introduce, :image
ただ、その前にとても気になる部分があるのでお聞きしたです。今回のエラーを直すためにどこが悪いのかを知りたくてterminalでログを確認しました。
上記2行目に記載されている、「Processing by Devise::RegistrationsController#create as HTML」は、Devise作成したRegistrationsControllerにおけるcreateアクションで処理が行われているということではないのでしょうか??
Deviseでコントローラを作成した時に作成されたのが、users/regstration_controller.rbだったので、その中のcreateアクション中に記載すればいいと考えたのですが・・・これじゃダメなんですね・・
######deviseで作成したコントローラは以下のコマンドで作成しました。
「rails g devise:controllers users」
###8zcaさんのアドバイスから発見した誤り
「registrations」と記載するべきところを「regisration」と、誤って記載していた。というよりはデフォルトでこの設定になっていた気がする。ルーティングを編集した覚えがないため。
以下、修正結果。
config/routes.rb
rails5
1 devise_for :users, controllers:{ 2 -)regisrations: 'users/regisrations' 3 +)registrations: 'users/registrations', 4 sessions: 'users/sessions' 5 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/07/28 10:04
2019/07/28 15:43
退会済みユーザー
2019/07/29 02:04
2019/07/29 13:43
退会済みユーザー
2019/07/30 12:52 編集
2019/07/30 15:48
退会済みユーザー
2019/07/31 05:35
退会済みユーザー
2019/07/31 05:54