LoginSignup
0

More than 3 years have passed since last update.

rubyのdeviseを後付けで、ユーザー登録エラーNo route matches [POST] "/people/sign_up.person"の解決方法

Posted at

deviseを最初からインストールしている記事はあるものの、追加でしかもmodel名のuserを使わない記事が皆無だったので後学者の為に。

環境
ruby 2.6.3
rails 5.2.4

作業時間を報告するアプリを作成。

先にpersonをmodelとして、複数形としてpeopleを使用。
一応これで課題は終了。

応用追加課題として、先のアプリに後付けで、
ログイン機能のdeviseを追加実装。

以下状況
gem deviseとdevise viewをインストールし、devise controllersはインストールせず。
personモデルはそのままにして、deviseのuserモデルのカラムを利用する為、userモデルを一度インストール。
インストールしたuserのmigrationファイルに記載されているカラムとfirst_name&second_nameを追加してファイル名をrenameし、migrate。
利用し終わったので、userモデルは削除。

ログイン画面と登録画面を実装して、動作確認時にエラー

エラー文
No route matches [POST] "/people/sign_up.person"

該当箇所

registration/new.html.haml
= form_for(resource, as: resource_name, url: new_person_registration_path(resource_name)) do |f|
devise/shared/_links.html.haml
- if devise_mapping.registerable? && controller_name != 'registrations'
  = link_to "Sign up", new_registration_path(resource_name)
$ rails routes
   new_person_registration GET    /people/sign_up(.:format) devise/registrations#new
  edit_person_registration GET    /people/edit(.:format)      devise/registrations#edit
       person_registration PATCH  /people(.:format)           devise/registrations#update
                           PUT    /people(.:format)           devise/registrations#update
                           DELETE /people(.:format)           devise/registrations#destroy
                           POST   /people(.:format)           devise/registrations#create

create画面に飛ばすべき所をnewパスにしていた。

registration/new.html.haml
= form_for(resource, as: resource_name, url: person_registration_path(resource_name)) do |f|

path修正

devise/shared/_links.html
- if devise_mapping.registerable? && controller_name != 'registrations'
  = link_to "Sign up", new_registration_path(resource_name)

こちらもルーティングに従い修正(person追加)

- if devise_mapping.registerable? && controller_name != 'registrations'
  = link_to "Sign up", new_person_registration_path(resource_name)

修正するも次のエラー分
ActionController::UnknownFormat in Devise::RegistrationsController#new

受け取るパラメーターをfirst_nameとsecond_nameを指定していなかった為
こちらを参考にして、
https://aliceblog1616.com/devise_parameter_sanitizer%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%E3%81%A8%E3%81%AF%EF%BC%9F/

class ApplicationController < ActionController::Base
  before_action :authenticate_person!
  before_action :configure_permitted_parameters, if: :devise_controller?

protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :second_name])
    devise_parameter_sanitizer.permit(:sign_in, keys: [:first_name, :second_name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :second_name])
  end
end

deviseのcontrollersをインストールしてないが、deviseが裏で作動しているので、if: :devise_controller?も記述。
エラー分変わらず。

ActionController::UnknownFormat in Devise::RegistrationsController#new

https://stackoverflow.com/questions/19193222/rails-4-devise-3-1-1-actioncontrollerunknownformat-in-deviseregistrationscon
を参考に

registration/new.html.haml
= form_for(resource, as: resource_name, url: person_registration_path(resource_name)) do 

から
(resource_name)を消す。

registration/new.html.haml
= form_for(resource, as: resource_name, url: person_registration_path) do |f|

修正

$ rails s

再起動忘れずに。
無事開通!

動作確認する前にroutesでしっかりpath確認して、parameterで追加カラムを指定する。
この工程を忘れやすいので、改めていい勉強になりました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0