设计错误“未定义的局部变量或方法`resource_class'”

时间:2016-05-27 13:28:43

标签: ruby-on-rails devise

我遇到与undefined local variable or method `resource_class' in devise相同的问题。问题是代码已经像答案一样设置了。这是路线问题吗?我应该去别处看看吗?

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

=&GT; _links.html.slim

.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}
  

#&lt;#:0x007ff25695fce0&gt;

的未定义方法`users_path'

前面是语法错误吗?

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

或者是我的routes.rb =&gt;

中的阻止
devise_for :users, :controllers => { 
  :registrations => "registrations",
  :sessions => "sessions",
  :confirmations => "confirmations", 
  :omniauth_callbacks => "callbacks" }
devise_for :admins

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我认为问题与设计无关:Devise Readme未提及为devise_for

中使用的模型创建路线

users_path启用config/routes.rb辅助方法,其中包含以下行:

get '/users', to: 'users#index', as: :users

或者很快,因为您可能需要更多路线:

resources :users, only: [:index] # you can skip the :only parameter if you need all restful routes.

查看Rails Routing from the Outside In了解详情。