5
5

More than 5 years have passed since last update.

Deviseを使ったサインアップがこける。

Posted at

やりたいこと

deviseを使ってログイン機能を実装しており、Sign_upするときに、emailとpasswordだけじゃなく、usernameも登録のときに入力させたいなと思った。

app/view/devise/registrations/new.html.erb

<h2>Sign up</h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :username, required: true %> #ここを追記
    <%= f.input :email, required: true %>
    <%= f.input :password, required: true %>
    <%= f.input :password_confirmation, required: true %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

追記してみたが、サインアップでこけてしまう。

解決策

パラメータがきちんと渡っていなかった。

app/controllers/application_controller.rb

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

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :username
  end
end

参考

ruby - Adding Username to devise rails 4 - Stack Overflow

plataformatec/devise#strong-parameters

5
5
0

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
5
5