undefined local variable or method `resource_class' in devise

倾然丶 夕夏残阳落幕 提交于 2019-12-11 02:38:34

问题


I am trying add a attribute called username to devise but rails shows me an error in the following line of code I am using rails 4.0.0 and devise 3

 def configure_permitted_parameters
       devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username,    :email, :password, :password_confirmation) } 
      end

Controller application

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters if :devise_controller?

  protected

  def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) } 
  end


end

回答1:


Try:

before_action :configure_permitted_parameters, if: :devise_controller?

or

before_action :configure_permitted_parameters, :if => :devise_controller?

Ref: https://github.com/plataformatec/devise/issues/2372 & http://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks




回答2:


You mistyped a comma before if:

before_action :configure_permitted_parameters, if: :devise_controller?



回答3:


I have the same problem here. The issue is that the code was already set like your answers. What's going wrong? Should I look elsewhere?

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  before_action :set_locale
  before_action :set_instances
  before_action :configure_permitted_parameters, if: :devise_controller?
  before_action :mixpanel

  def set_locale
   I18n.locale = params[:locale] || I18n.default_locale
  end

  def redirect_to_back
   begin
    redirect_to :back
    rescue ActionController::RedirectBackError => e
    redirect_to root_path
    end
  end

  def set_instances
    @new_event ||= Event.new(title: ".......", capacity: 50, start_date: Time.zone.now, start_time: Time.zone.now, end_time: Time.zone.now)
    @featured_quizz ||= Questionnaire.where('featured IS TRUE')
  end

   helper_method :resource_name, :resource, :devise_mapping

  protected


  def resource_name
   :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << [:name, :firstname, :lastname, :phone]
    devise_parameter_sanitizer.for(:account_update) << [:name, :firstname, :lastname, :image, :phone]
  end

end

And where it shows the error _links.html.slim

undefined local variable or method `resource_class' for #<#:0x007f82f02c8218>

.row.text-center
.col-md-10.col-md-offset-1

  - if devise_mapping.omniauthable?
    - resource_class.omniauth_providers.each do |provider|
      = link_to omniauth_authorize_path(resource_name, provider), class:"btn btn-fb btn-block callout callout-fb"
        span.fa.fa-facebook-official<>
        span Connectez vous avec #{provider.to_s.titleize}

Is it a syntax error in front ?

  li = link_to "Votre Compte", resource, as: resource_name, url: session_path(resource_name), remote: true, data: { toggle: "modal", target: "#login-modal" }


来源:https://stackoverflow.com/questions/29710717/undefined-local-variable-or-method-resource-class-in-devise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!