質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

1769閲覧

deviseのupdateが成功しなくて困っています・・・助けてください・・・

k_yusuke

総合スコア19

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

3クリップ

投稿2019/08/07 06:40

編集2019/08/10 11:17

前提・実現したいこと

いつもお世話になっております。今回もよろしくお願いいたします。
Railsでユーザーを新規登録・ユーザー情報を編集するWEBアプリを作っています。
ユーザーのCRUDにdeviseを使っているのですが、
restigations_controllerのupdateメソッドが失敗するため、app/views/restigations/edit.html.hamlへrenderされていまい、ユーザー情報の更新ができません。
画像を入れずに、submitしてみたり、フォームの指示通り、パスワードを空にしてみたりしたのですがだめでした
deviseのソースコードを見る限りcreateアクションとupdateアクションは対になっているのに、createは成功してupdateが失敗してしまうのが理解できないところです...
皆様のお知恵をお貸しいただけるとありがたいです。
###update時のターミナルのログ

Started PATCH "/users" for 36.3.213.27 at 2019-08-10 11:11:34 +0000 Cannot render console from 36.3.213.27! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Users::RegistrationsController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"D7vCZVEMy9lTHg7OsmqqscvZp0BxYir0x9RLbxPudK/+8xikcpDf0Pe2LYBUMbXv69GvOsaFYfXWY+JK1Un9Qw==", "user"=>{"username"=>"yusuke", "email"=>"yusuke@yahoo.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"更新する"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 93], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 93], ["LIMIT", 1]] (0.1ms) begin transaction (0.0ms) rollback transaction Rendering users/registrations/edit.html.haml within layouts/application Rendered users/registrations/edit.html.haml within layouts/application (10.2ms) Rendered layouts/_head.html.haml (44.2ms) Rendered layouts/_header.html.haml (8.0ms) Rendered layouts/_flash.html.haml (1.2ms) Completed 200 OK in 80ms (Views: 71.6ms | ActiveRecord: 0.3ms)

該当のソースコード

app/controller/users/restigations_controller.rb

#frozen_string_literal: true class Users::RegistrationsController < Devise::RegistrationsController before_action :configure_sign_up_params, only: [:create] before_action :configure_account_update_params, only: [:update] # GET /resource/sign_up def new super end # POST /resource def create super end # GET /resource/edit def edit super end # PUT /resource def update super end # DELETE /resource def destroy super end # GET /resource/cancel # Forces the session data which is usually expired after sign # in to be expired now. This is useful if the user wants to # cancel oauth signing in/up in the middle of the process, # removing all OAuth session data. # def cancel # super # end protected def update_resource(resource, params) resource.update_without_password(params) end # If you have extra params to permit, append them to the sanitizer. def configure_sign_up_params devise_parameter_sanitizer.permit(:sign_up) do |u| u.permit(:username, :email, :password, :password_confirmation,:userimage) end end # If you have extra params to permit, append them to the sanitizer. def configure_account_update_params devise_parameter_sanitizer.permit(:account_update) do |u| u.permit(:username, :email, :password, :password_confirmation, :userimage) end end # The path used after sign up. def after_sign_up_path_for(resource) super(resource) end # The path used after sign up for inactive accounts. def after_inactive_sign_up_path_for(resource) super(resource) end end

app/controller/application_controller.rb

class ApplicationController < ActionController::Base # protect_from_forgery with: :exception before_action :set_search def set_search @search = Tour.ransack(params[:q]) @search_tours = @search.result.page(params[:page]) end protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) << :username devise_parameter_sanitizer.for(:sign_up) << :userimage devise_parameter_sanitizer.for(:account_update) << :username devise_parameter_sanitizer.for(:account_update) << :userimage end def check_user_login? unless current_user flash.now[:notice]="ログインしてください" redirect_back(fallback_location: root_path) end end end

app/views/users/restigations/edit.html.haml

%h3 ユーザー編集ページ #{resource_name.to_s.humanize} = simple_form_for(resource, as: resource_name, url: registration_path(resource_name) ,html: {multipart: true}) do |f| = f.error_notification .form-inputs = f.input :username, required: true, autofocus: true, input_html: { autocomplete: "username",class:"form-control" } = f.input :email, required: true, autofocus: true, input_html: {class:"form-control" } - if devise_mapping.confirmable? && resource.pending_reconfirmation? %p Currently waiting confirmation for: #{resource.unconfirmed_email} = f.input :password, hint: "leave it blank if you don't want to change it", required: false, input_html: { autocomplete: "new-password",class:"form-control" } = f.input :password_confirmation, required: false, input_html: { autocomplete: "new-password",class:"form-control mb-3" } = f.input :userimage, as: :file, input_html: { class:"mb-3" } .form-actions = f.button :submit, "更新する" %p.mt-3 #{link_to "アカウントを削除する", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete} = link_to "キャンセル", root_path

app/views/users/restigations/new.html.haml

%h2.page_title ユーザー登録ページ = simple_form_for(resource, as: resource_name, url: registration_path(resource_name) ,html: {multipart: true}) do |f| = f.error_notification .form-inputs.form_group = f.input :username, required: true, autofocus: true, input_html: { autocomplete: "username",class:"form-control" } = f.input :email, required: true, autofocus: true, input_html: { autocomplete: "email",class:"form-control"} = f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length), input_html: { autocomplete: "new-password",class:"form-control" } = f.input :password_confirmation, required: true, input_html: { autocomplete: "new-password",class:"form-control mb-3" } = f.input :userimage, as: :file = f.button :submit, "登録する", input_html: { class:" btn btn-primary " } = link_to 'Signin with Google',user_google_oauth2_omniauth_authorize_path = render "users/shared/links"

app/config/routes

Rails.application.routes.draw do mount RailsAdmin::Engine => '/admin', as: 'rails_admin' root 'static_pages#home' get '/about'=>'static_pages#about' get '/search'=>'static_pages#search' devise_for :users, controllers: { registrations: 'users/registrations', sessions: "users/sessions", omniauth_callbacks: "users/omniauth_callbacks" } resources :users, :only => [:show,:follow] resources :tours, :only => [:index,:new,:create,:show, :edit,:update,:destroy] resources :relationships, only: [:create, :destroy] resources :users do member do get :followings, :followers,:favorite end end resources :tours do resources :likes, :only => [:create,:destroy] resources :favorites, only: [:create, :destroy] resources :comments, :only => [:create,:destroy] end end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

k_yusuke

2019/08/10 11:15

tktktさん回答ありがとうございます! 返信が大変遅くなってしまい申し訳ございません!! update時にターミナルに表示されているログを追記しました!!! 返信遅くなってしまったのですが、お力をお貸しいただけるとありがたいです!
k_yusuke

2019/08/11 06:12

回答ありがとうございます update_resource(resource, params)メソッド内の最後にbinding.pryをかませてみたところ、 => {"username"=>"monkey", "email"=>"monkey@monkey", "userimage"=> #<ActionDispatch::Http::UploadedFile:0x00007f296604f7b8 @content_type="image/jpeg", @headers= "Content-Disposition: form-data; name=\"user[userimage]\"; filename=\"mono.jpg\"\r\nContent-Type: image/jpeg\r\n", このように、名前。メールアドレス、画像のデータがparamsから拾えました! パスワードに関してはにゅうりょくの有無に関わらずparamsで拾ってくれません。これが原因の可能性が大きいですか???
urbainleverrier

2019/08/11 06:46

上記のメソッド`update_without_password `の最後ではresultを返します。 binding.pryの時に`update_without_password(params)`を実行すると結果はどうなりますか?
urbainleverrier

2019/08/11 06:51

ちなみに該当メソッドはgit clone後、git grep ~することで簡単に見つけられます。他に気になるメソッドがあれば検索語、コードを読んでみてください。
k_yusuke

2019/08/11 06:54

コンソール内でログイン中ユーザーにそのメソッドを使うと [3] pry(#<Users::RegistrationsController>)> current_user.update_without_password(params) (0.1ms) begin transaction (0.0ms) rollback transaction => false となり失敗するようです
k_yusuke

2019/08/11 06:56

なるほど!!そんな方法があったんですね!!ソースコード最初から読んでいました。 ソースコードもさらに読み込んでみます!
urbainleverrier

2019/08/11 17:45 編集

Deviceが適用されるモデルはありますか? そのモデルでupdateできますか?
k_yusuke

2019/08/12 02:55

deviseで適応されるモデルというとuserモデルだと思います コンソール上でuserに対してupdateを行ってもトランザクションがうまくいかないです。 update_attributesにおいても同様なのでバリデーションの問題ではないようなのですが。。。
urbainleverrier

2019/08/12 05:47

では、問題はdeviceではなく、モデルにあるようです。 こちらの質問は閉じて、Userモデルのupdateができないという問題で質問をした方がいいと思います。
k_yusuke

2019/08/12 06:29

わかりました!!整理しなおして質問してみます!! ありがとうございました!!!
k_yusuke

2019/08/14 06:42

お久しぶりです!rails db:migrate:resetしたところ何故か上手くいきました!! おそらくこのコマンドが原因で解決したのかは定かではないですが。。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問