#概要
gem 'devise' を使ってユーザー新規登録の実装をしています。
devise用のテーブルにuser_nameカラムを追加してあります。
表題の通り、以下の実装で registration#create で UnknownFormat エラーを吐かれてしまいます。
(Devise::RegistrationsController の createアクションを使いたい)
理由は何故でしょうか。
#質問の背景
以下の registration#create の実装で期待通りの挙動ができたのですが、
使い方が芳しくないと指摘を受けたため。
def create @user = User.new(registration_params) if @user.save redirect_to new_user_session_path, success:'登録完了' else render :new end end def registration_params params.require(:user).permit(:user_name, :email, :password, :password_confirmation) end
###application_controller.rb
class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:user_name]) end end
###registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController before_action :configure_sign_up_params, only: [:create] def new super @user = User.new end def create #ActionController::UnknownFormat in Users::RegistrationsController#create super end protected def registration_params params.require(:user).permit(:email, :password, :password_confirmation) end def configure_sign_up_params devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) end
###config/routes.rb
Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html devise_for :users, skip: :all devise_scope :user do root to: "users/sessions#new", as: :new_user_session get "sign_up", :to => "users/registrations#new", as: :new_user_registration post "sign_up", :to => "users/registrations#create", as: :user_registration get "sign_out", :to => "users/sessions#destroy", as: :destroy_user_session get "password/new", :to => "users/password#new", as: :new_user_password end end
##app/views/users/registrations/new.html.erb
h2>新規登録</h2> <%= form_for(resource, as: resource_name, url: new_user_registration_path(resource_name)) do |f| %> <%= render "devise/shared/error_messages", resource: resource %> <div class="field"> <%= f.label :user_name %><br /> <%= f.text_field :user_name, autofocus: true, autocomplete: "name" %> </div> <div class="field"> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true, autocomplete: "email" %> </div> <div class="field"> <%= f.label :password %> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %> characters minimum)</em> <% end %><br /> <%= f.password_field :password, autocomplete: "new-password" %> </div> <div class="field"> <%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation, autocomplete: "new-password" %> </div> <div class="actions"> <%= f.submit "新規登録" %> </div> <% end %> <%= render "users/shared/links" %>
あなたの回答
tips
プレビュー