设计中未定义的局部变量或方法`resource_class'

时间:2015-04-17 22:54:32

标签: ruby-on-rails devise ruby-on-rails-4.1 rails-4-2-1

我正在尝试添加一个名为username的属性来设计,但rails在下面的代码行中显示错误 我正在使用rails 4.0.0并设计3

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

控制器应用程序

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

3 个答案:

答案 0 :(得分:1)

尝试:

before_action :configure_permitted_parameters, if: :devise_controller?

before_action :configure_permitted_parameters, :if => :devise_controller?

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

答案 1 :(得分:1)

您之前输错了逗号:

before_action :configure_permitted_parameters, if: :devise_controller?

答案 2 :(得分:0)

我在这里遇到同样的问题。问题是代码已经设置好了你的答案。出了什么问题?我应该去别处看看吗?

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

它显示错误_links.html.slim

  

#&lt;#:0x007f82f02c8218&gt;

的未定义局部变量或方法`resource_class'
.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}

前面是语法错误吗?

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