前提・実現したいこと
いつもお世話になっております。今回もよろしくお願いいたします。
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
あなたの回答
tips
プレビュー