LoginSignup
0
1

More than 3 years have passed since last update.

SNS認証時のエラーについて

Posted at

経緯

ウィザード形式のユーザー登録機能実装後、
SNS認証を追加するために
registrations_controller内に必要な記述をした。
(userモデルは記述済み。)

controller
class Users::RegistrationsController < Devise::RegistrationsController

#一部省略

def create
    if params[:sns_auth] == 'true'
      pass = Devise.friendly_token
      params[:user][:password] = pass
      params[:user][:password_confirmation] = pass
    end
    super                               #ここまでがSNS認証実装に追記した記述
    @user = User.new(sign_up_params)    #ここからウィザード形式のための記述
      unless @user.valid?
        render :new and return
      end
      session["devise.regist_data"] = {user: @user.attributes}
      session["devise.regist_data"][:user]["password"] = params[:user][:password]
      @profile = @user.build_profile
      render :new_profile
  end

#一部省略

end

エラー内容

新規登録を行ってみたところ下記のようなエラーメッセージが出る。

AbstractController::DoubleRenderError in Users::RegistrationsController#create

同一アクション内にrenderもしくはredirectが複数回使われていますよ!という内容。
renderの後にand returnで処理を終わらせているのになぜ?となる。。

エラー原因

SNS認証を実装するために追記した記述の最後にある
superによるもの。
(superとは、継承元のクラスにある、superを記述したメソッドと同名のメソッドを呼び起こすことができる。)

呼び起こし先は下記である。
(https://github.com/heartcombo/devise/blob/master/app/controllers/devise/registrations_controller.rb) 参照元

RegistrationsController
class Devise::RegistrationsController < DeviseController

# 一部省略

def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
    end
  end

# 一部省略

end

この中のrespond_withがどうやらエラーの原因みたい。
respond_withは、renderやredirectの役割を持っているため。
よってsuperで呼び起こしたメソッド内でrender or redilectしようとしたけど、その後の記述でまたrenderがあり
どっち?となりエラーが起きたと考えられる。

0
1
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
0
1