@uot (uo yu)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

ユーザーの新規登録時に「undefined method `nickname' for nil:NilClass」というエラーが発生してしまう

Q&A

Closed

【解決したいこと】

railsにて、下記のエラー内容を解決し、ユーザーの新規登録ができるようになりたいです。
初歩的なエラーかと存じますが中々解決ができません。。

【エラー内容】

※エラー画像
https://gyazo.com/d1fb9c71bd68a796e7095fa1e8df1b08

*NoMethodError in Users::Registrations#confirm
Showing /Users/uot/projects/subscription/app/views/devise/registrations/confirm.html.erb where line #14 raised:
undefined method `nickname' for nil:NilClass

Parameters:
{"authenticity_token"=>"9VD2gIg0OF62TEethtxK1M7Pbcb9Vv/Xi2pQDUxzo8inWWIhj6qpmoARk8oq8HrV3VuBUEq6TJqUT7WSG1oTww==",
"user"=>{"nickname"=>"tarou", "email"=>"u@gmail.com", "password"=>"[FILTERED]", "check"=>"あり"},
"commit"=>"確認画面"}*

【エラー発生経緯】

実装したい流れは、このような形になります。
ユーザー新規登録情報を投入→確認画面→登録完了

今回は、ユーザー新規登録情報を投入し、確認画面が表示される前にエラーが発生してしまいます。

【コード】

◇エラーの対象のコード

①confirm.html.erb (確認画面)

<%= render "shared/header" %>
<body>


<div class = "confirm">
  <h2 class ="user-check">入力情報の確認</h2>

  <%= form_with model: @user, url: user_registration_path do |f| %>
<div class='confirmform-wrap'>

<div class = "chack-box">
 <p>ニックネーム</p>
 <div class="box17">
    <%= f.text_field :nickname, value: @user.nickname,class:"check-letter", style: "border: none",readonly: true %>
 </div>
</div>

<div class = "chack-box">
 <p>email</p>
 <div class="box17">
  <%= f.text_field :email, value: @user.email,class:"check-letter",style: "border: none",readonly: true %>
 </div>
</div>


 <div class = "chack-box">
  <p>password</p>
  <div class="box17">
   <%= f.text_field :password, value: @user.password ,class:"check-letter" ,style: "border: none",readonly: true %>
  </div>
 </div>

 <div class = "chack-box">
   <p>お知らせ</p>
  <div class="box17">
   <%= f.text_field :check, value: @user.check,class:"input-default" ,class:"check-letter" ,style: "border: none",readonly: true %>

  </div>
 </div>

  <div class = "confirm.submit">
   <div class = "submit-box">
    <%= f.submit "修正する", name: :back , :class=> "submit-botan" %>
    <%= f.submit "登録する" ,:class=> "submit-botan"%>
     </div>
  </div>

</div>
  <% end %>
</div>
</body>

②new.html.erb (新規登録画面)

<%= render "shared/header" %>


<h2 class="views-title">ユーザー新規登録</h2>

<%= form_with model: @user, url: users_sign_up_confirm_path(@user),  local: true do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

<div class='form-wrap'>

  <div class = "form-box">
    <%= image_tag "user.png", class:"newicon-image" %>
    <div class="form-group">
      <div class='form-text-wrap'>
        <label class="form-text">ニックネーム</label>
      </div>
        <%= f.text_area :nickname, id:"nickname", placeholder:" 例) Tarou",  class:"input-default",maxlength:"40" %>
    </div>
  </div>

  <div class = "form-box">
    <%= image_tag "email.png", class:"newicon-image" %>
    <div class="form-group">
      <div class='form-text-wrap'>
       <label class="form-text">email</label>
      </div>
        <%= f.email_field :email, class:"input-default", id:"email", placeholder:" PC・携帯どちらでも可", autofocus: true %>
    </div>
  </div>

<div class = "form-box">
    <%= image_tag "password.jpeg", class:"newicon-image" %>
    <div class="form-group">
      <div class='form-text-wrap'>
       <label class="form-text">Password</label>
      </div>
        <%= f.password_field :password, class:"input-default", id:"password", placeholder:" 6文字以上の半角英数字" %>
    <p class='info-text'>※英数混合の6文字以上</p>
    </div>
  </div>


  <div class = "form-box">
    <%= image_tag "check.png", class:"newicon-image" %>
    <div class="form-group">
      <div class='form-text-wrap'>
       <label class="form-text">お知らせ</label>
      </div>
       <label><%= f.radio_button :check, "あり" %> あり </label>
       <label><%= f.radio_button :check, "なし" %> なし </label>
    </div>
  </div>

  <div class="actions">
    <%= f.submit "確認画面" , :class=> "submit-botan" %>
  </div>

</div>
<% end %>

②registrations_controller.rb (ユーザーコントローラ)

controller
class Users::RegistrationsController < Devise::RegistrationsController


def destroy
  @user = User.find(params[:format]) 
      @user.destroy
      flash[:notice] = 'ユーザーを削除しました。'
      redirect_to :root
end

  def create
    @user = User.new(sign_up_params)
    render :new and return if params[:back]
    super
  end

  def update_without_current_password(params)
    if params[:password].blank? && params[:password_confirmation].blank?
      params.delete(:password)
      params.delete(:password_confirmation)
    end
    update(params)
  end

  def update
    if current_user.update(user_params)
      redirect_to root_path
    else
      render :edit
    end
  end

  protected
    def update_resource(resource, params)
      resource.update_without_password(params)
    end

  # 新規追加
  def confirm
    @user = User.new(sign_up_params)

  end

  # 新規追加
  def complete
  end

  # アカウント登録後
  def after_sign_up_path_for(resource)
    users_sign_up_complete_path(resource)
  end

  private
  def user_params
    params.require(:user).permit(:nickname, :email, :password, :check)
  end

end
0 likes

3Answer

class Users::RegistrationsController < Devise::RegistrationsController

  def confirm
    @user = User.new(sign_up_params)
    Rails.logger.debug("#"*20)
    Rails.logger.debug(sign_up_params)
    Rails.logger.debug(@user)
    Rails.logger.debug("#"*20)
  end

などとまずしてみて、期待通りの内容かどうかまずは見てみるとか?

0Like

This answer has been deleted for violation of our Terms of Service.

Looks like the nickname value is being called before the user object is created. Checking that part of the registration code and handling nil values should help fix the error. A Stylish Nickname Generator can also give new users some creative nickname options.

0Like

Your answer might help someone💌