undefined method `session_path'

一世执手 提交于 2019-12-05 09:48:06

问题


I am using Rails + Devise + OmniAuth + Google OAuth2.

My user model (user.rb) contains:

devise :registerable, :omniauthable, :omniauth_providers => [:google_oauth2]

My routes.rb look like:

Rails.application.routes.draw do
  devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' }
  devise_scope :user do
      get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
      post 'sign_in', :to => 'devise/session#create', :as => :user_session
      get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
  end

  get 'services', to: 'static_pages#services'
  get 'my_account', to: 'my_account#index'
  get 'invite', to: 'invite#show'
  get 'invite/:id', to: 'invite#show'

  root 'static_pages#home'
end

When I go to /sign_in, I get an exception like:

undefined method `session_path' for #<#<Class:0x007f9b7173af28>:0x007f9b713d8da8>

in:

~/.rvm/gems/ruby-2.1.1/gems/devise-3.2.4/app/views/devise/sessions/new.html.erb

in line:

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>

If I add :database_authenticatable to user.rb it all starts working, but I want my users to be able to sign-in through Google OAuth2 only, so I don't want :database_authenticable. It looks like session_path is not available for some reason, but I am not sure why and how to make it available.

Thanks, Jen


回答1:


You need to reboot the rails server. That was the solution for me.




回答2:


I do believe that, as you use devise_scope for the sessions paths, you need to add skip to your devise_for call, like so:

devise_for :users, skip: [:sessions], controllers: { omniauth_callbacks: 'omniauth_callbacks' }

Doing so will not generate the route helpers for the sessions controller



来源:https://stackoverflow.com/questions/24420942/undefined-method-session-path

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