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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

Ruby on Rails

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

Q&A

0回答

1101閲覧

【devise】registrationコントローラのcreateアクションでsuperが使えない

noydmt_1021

総合スコア13

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/04/23 09:15

#概要
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" %>

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問