前提・実現したいこと
railsでdeviseを用いて会員認証を実装しています。
その際にregistrations/editでユーザーの会員情報を変更する際に更新ボタン(リンク?)をクリックした際に
Processing by モデル名::RegistrationsController#destroy as HTML
が走ってしまい。期待しているUpdateアクションが動かないというのが問題です。これを、
Processing by モデル名::RegistrationsController#update as HTML
というようにUpdateアクションを呼び出し、モデルのデータを更新できるようにしたいです。
発生している問題・エラーメッセージ
前述の通りですが、deviseで生成されるUpdateアクションを押すとDestoryアクションが走ってしまうという問題が発生しています。
Processing by モデル名::RegistrationsController#destroy as HTML
該当のソースコード
関連すると思われる、registrations_controllerとregistrations/edit.html.haml、routesを記載します。
######registrations_controller
ruby
1class Stores::RegistrationsController < Devise::RegistrationsController 2 before_action :configure_sign_up_params, only: [:create] 3 before_action :configure_account_update_params, only: [:update] 4 5 # GET /resource/sign_up 6 # def new 7 # super 8 # end 9 10 # POST /resource 11 # def create 12 # super 13 # end 14 15 # GET /resource/edit 16 # def edit 17 # super 18 # end 19 20 # PUT /resource 21 # def update 22 # super 23 # end 24 25 # DELETE /resource 26 # def destroy 27 # super 28 # end 29 30 # GET /resource/cancel 31 # Forces the session data which is usually expired after sign 32 # in to be expired now. This is useful if the user wants to 33 # cancel oauth signing in/up in the middle of the process, 34 # removing all OAuth session data. 35 # def cancel 36 # super 37 # end 38 39 # protected 40 41 # If you have extra params to permit, append them to the sanitizer. 42 def configure_sign_up_params 43 devise_parameter_sanitizer.permit(:sign_up, keys: [登録の際に許可するパラメーター]) 44 end 45 46 # If you have extra params to permit, append them to the sanitizer. 47 def configure_account_update_params 48 devise_parameter_sanitizer.permit(:account_update, keys: [更新の際に許可するパラメーター]) 49 end 50 51 52 # The path used after sign up for inactive accounts. 53 # def after_inactive_sign_up_path_for(resource) 54 # store_show_path(resource.id) 55 # end 56end
######registrations/edit.html.haml
ruby
1%header 2 .divece-wrapper 3 %h2 4 #{resource_name.to_s.humanize}情報編集 5 = form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| 6 = render "stores/shared/error_messages", resource: resource 7 .field 8 = f.label :email, class:'device__label' 9 %br/ 10 = f.email_field :email, autofocus: true, autocomplete: "email", class:'device__textfield' 11 - if devise_mapping.confirmable? && resource.pending_reconfirmation? 12 %div 13 現在確認待ち中: #{resource.unconfirmed_email} 14 15 .field 16 .must 17 = f.label :name, class:'device__label' 18 %h3 必須 19 = f.text_field :name, class:'device__textfield' 20 .field 21 .must 22 = f.label :tel 23 %h3 必須 24 = f.text_field :tel, autocomplete: "phone_number", placeholder: '例)09012345678', class:'device__textfield' 25 .field 26 = f.label :password, class:'device__label' 27 %br/ 28 %i (変更しない場合は空白のままにします) 29 = f.password_field :password, autocomplete: "new-password", class: 'device__textfield' 30 - if @minimum_password_length 31 %em 32 = @minimum_password_length 33 文字以上 34 .field 35 = f.label :password_confirmation, class:'device__label' 36 = f.password_field :password_confirmation, autocomplete: "new-password", class: 'device__textfield' 37 .field 38 = f.label :current_password, class:'device__label' 39 %br/ 40 %i (パスワードを変更するには、現在のパスワードが必要です) 41 = f.password_field :current_password, autocomplete: "current-password", class: 'device__textfield' 42 .actions 43 = f.submit "更新", class: 'device__btn' 44 .device-btn 45 = link_to "戻る", :back, class: 'device__btn' 46 %p 47 .device-cancel 48 サービスにご不満ですか? 49 .delete-btn 50 #{button_to "アカウントの削除", registration_path(resource_name), data: { confirm: "削除してもよろしいですか?" }, method: :delete, class:'account-delete-btn'}
routes.rb
ruby
1Rails.application.routes.draw do 2 mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 3 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 4 root to: 'home#index' 5 devise_for :モデル名Andy, controllers: { 6 sessions: 'stores/sessions', 7 passwords: 'stores/passwords', 8 registrations: 'stores/registrations', 9 } 10 devise_for :モデル名B, controllers: { 11 sessions: 'users/sessions', 12 passwords: 'users/passwords', 13 registrations: 'users/registrations', 14 } 15 16 get 'home/search', to: 'home#search', as: 'search_menu' 17 post 'home/new', to: 'home#new' 18 resources :home, only: [:index, :show] 19 20 get 'cart/bought', to: 'cart#bought', as: 'cart_bought' 21 post 'cart/edit', to: 'cart#edit' 22 resources :cart, only: [:show] 23 post 'cart/:id/pay', to:'cart#pay', as: 'cart_pay' 24 25 get 'cards/success', to: 'cards#success' 26 resources :cards, only: [:new, :create, :show, :destroy] do 27 end 28 29 post 'store/:id/complete', to:'store#complete', as: 'store_complete' 30 get 'store/:id/archive', to:'store#archive', as: 'store_archive' 31 resources :store, only: [:show] 32 33 post 'menu/delete', to:'menu#delete' 34 post 'menu/add', to:'menu#add' 35 resources :menu, only: [:show] 36end
試したこと
アクションを強制的に呼び出せないものかと思いまして、form_forのURLの引数を
ruby
1= form_for(resource, as: resource_name, url: {controller: 'users/registrations', action:'update'}, html: { method: :put }) do |f|
と変更しましたがエラーは変わりませんでした。
補足情報(FW/ツールのバージョンなど)
Rails 5.2.4.3
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
devise (4.7.2, 4.7.1)
Mojave 10.14.6
何卒よろしくお願いします。
あなたの回答
tips
プレビュー