Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

deviseにおけるserialize_from_sessionのエラー

解決したいこと

Ruby on Railsでdeviseを利用し、2パターンのユーザー管理機能を実装しています。
ユーザー新規登録を実装したはエラーが起きなかったものの、パターン別でviewの表示を変える機能を実装している際に片方のユーザーの新規登録等の全てのページでエラーが発生してしまいました。

①企業側ユーザー key_user
②取引先ユーザー order_user

発生している問題・エラー

※現在理解していること
・serialize_from_sessionメソッドの呼び出しの際の引数の数が違う為、発生しているエラーなこと
※わからない事
・serialize_from_sessionメソッドはどのようなメソッドか。
・serialize_from_sessionメソッドはどこに記載されているか。
・なぜ新規登録を実装した際は発生しなかったのに、view等の記載を変えた際にエラーが発生したのか

//localhost:3000/key_users/sign_up
ArgumentError in Devise::RegistrationsController#new

wrong number of arguments (given 6, expected 2)


def serialize_from_session(key, salt)
          record = to_adapter.get(key)
          record if record && record.authenticatable_salt == salt
end

変更したview

views/items/index.html.erb
<% if key_user_signed_in? %>
  <div class='left-list'>
    <ul class='item-list'>
      <li><%= link_to '新規商品登録', new_item_path, class:"new-item-btn" %></li>
      <li><%= link_to '商品検索', "#", class:"item-serch-btn" %></li>
    </ul>
    <ul class='news-list'>
      <li><%= link_to '新規情報登録', "#", class:"new-news-btn" %></li>
      <li><%= link_to '情報検索', "#", class:"news-serch-btn" %></li>
    </ul>
    <ul class='stock-list'>
      <li><%= link_to '新規在庫登録', "#", class:"new-stock-btn" %></li>
      <li><%= link_to '在庫検索', "#", class:"stock-serch-btn" %></li>
    </ul>
  </div>
<% end %>

views/key_users/registrations/new.html.erb
<%= form_with model: @key_user, url: key_user_registration_path, class: 'registration-main', local: true do |f| %>
<div class="form-wrap">
  <div class="form-header">
    <h1 class="form-header-text">
      会員情報入力
    </h1>
  </div>

  <div class="form-group">
    <div class="form-text-wrap">
      <label class="form-text">名前</label>
      <span class="indispensable">必須</span>
    </div>
    <%= f.text_area :name, class:"input-default", id:"key_user_name", placeholder:"例)佐藤太郎" %>
    <p class='info-text'>スペース無しで名前を入力してください</p>
  </div>
  <div class="form-group">
    <div class="form-text-wrap">
      <label class="form-text">メールアドレス</label>
      <span class="indispensable">必須</span>
    </div>
    <%= f.text_area :email, class:"input-default", id:"key_user_email", placeholder:"例)keycoffee@keycoffee.co.jp" %>
    <p class='info-text'>社用メールアドレスをご登録ください</p>
  </div>
  <div class="form-group">
    <div class='form-text-wrap'>
      <label class="form-text">パスワード</label>
      <span class="indispensable">必須</span>
    </div>
    <%= f.password_field :password, class:"input-default", id:"key_user_encrypted_password", placeholder:"半角英数8桁以上" %>
    <p class='info-text'>半角数字と半角英字の両方を含めた8桁以上でご設定ください</p>
  </div>
  <div class="form-group">
    <div class='form-text-wrap'>
      <label class="form-text">パスワード(確認用)</label>
      <span class="indispensable">必須</span>
    </div>
    <%= f.password_field :password_confirmation, class:"input-default", id:"key_user_password-confirmation", placeholder:"半角英数8桁以上" %>
    <p class='info-text'>再度パスワードをご入力ください</p>
  </div>
  <div class="form-group">
    <div class='form-text-wrap'>
      <label class="form-text">社員番号</label>
      <span class="indispensable">必須</span>
    </div>
    <%= f.text_area :employee_number, class:"input-default", id:"key_user_employee_number", placeholder:"K111111" %>
    <p class='info-text'>半角大文字K+半角6桁の数字でご登録ください</p>
  </div>
  <div class="registrations-btn">
    <%= f.submit "会員登録" ,class:"register-btn" %>
  </div>
<% end %>
</div>
controllers/key_users/registrations_controllers.rb
frozen_string_literal: true

class KeyUsers::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]
  before_action :configure_account_update_params, only: [:update]

  GET /resource/sign_up
  def new
    super
  end

  POST /resource
  def create
    super
  end

  GET /resource/edit
  def edit
    super
  end

  PUT /resource
  def update
    super
  end

  DELETE /resource
  def destroy
    super
  end

  GET /resource/cancel
  Forces the session data which is usually expired after sign
  in to be expired now. This is useful if the user wants to
  cancel oauth signing in/up in the middle of the process,
  removing all OAuth session data.
  def cancel
    super
  end

  protected

  If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :employee_number])
  end

  If you have extra params to permit, append them to the sanitizer.
  def configure_account_update_params
    devise_parameter_sanitizer.permit(:account_update, keys: [:name, :employee_number])
  end

  The path used after sign up.
  def after_sign_up_path_for(resource)
    super(resource)
  end

  The path used after sign up for inactive accounts.
  def after_inactive_sign_up_path_for(resource)
    super(resource)
  end
end

自分で試したこと

他、追記するべきコードありましたらご教授くださいよろしくお願いいたします。

0 likes

No Answers yet.

Your answer might help someone💌