ruby-on-rails rails教學 - 將Devise Login設置為根頁面




4 Answers

要跟踪那些詢問錯誤的人Could not find devise mapping for path "/"有一個解決方法。

你會發現你的日誌中有一條線索可能會說:

[Devise] Could not find devise mapping for path "/".
This may happen for two reasons:

1) You forgot to wrap your route inside the scope block. For example:

  devise_scope :user do
    match "/some/route" => "some_devise_controller"
  end

2) You are testing a Devise controller bypassing the router.
   If so, you can explicitly tell Devise which mapping to use:

   @request.env["devise.mapping"] = Devise.mappings[:user]

所以我重新嘗試了這個方法,而是將它包裝起來(如@miccet suggets)在一個範圍塊中:

devise_scope :user do
  root to: "devise/sessions#new"
end

這對我來說很好

sign_in new_with_session

我為我的路線使用以下代碼:

devise_for :user, 
  :as => '', 
  :path_names => { 
    :sign_in => "", 
    :sign_out => "logout", 
    :sign_up => "register" 
  }

但是當我退出並轉到/logout我收到以下錯誤:

沒有路由匹配{:action =>“new”,:controller =>“devise / sessions”}

如何設置根路徑為:sign_in action?




root :to => "devise/sessions#new"

我需要設置默認的home root。 我覺得昨晚(在發布問題之前)我整夜都試過這個,但它現在正在運作。 如果您已註銷,Devise會嘗試將您重定向到我未定義的根路徑。




我得到這個與@VvDPzZ答案一起工作。 但我不得不稍微修改它

  devise_scope :business_owner do
    authenticated  do
      root to: 'pages#dashboard'
    end

    unauthenticated do
      root to: 'devise/sessions#new', as: 'unauthenticated_root'
    end
  end  

我不得不廣告to:在根路徑聲明中。 我還刪除了as: :authenticated_root因為我的應用程序中已經有一些位置引用了鏈接中的root_path 。 通過省略as: :authenticated_root部分,我不必更改任何現有鏈接。




使用rails 3.2devise 3.2.3我設法將我的主頁“ home #index ”(controller#action)設置為登錄頁面,進行以下更改。

#1在主頁上添加了登錄表單:

<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
    <%= f.input :email %>
    <%= f.input :password %>
    <%= f.button :submit %>
<% end %>

#2app / heldpers / application_helper.rb中添加了resource_name,resource和devise_mapping方法:

def resource_name
  :user
end

def resource
  @resource ||= User.new
end

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

#3創建自定義會話控制器app / controllers / users / sessions_controller.rb

class Users::SessionsController < Devise::SessionsController

   protected

   # This method tell sessions#create method to redirect to home#index when login fails.
   def auth_options
      { scope: resource_name, recall: 'home#index' }
   end

end

#4跳過會話路由並在config / routes.rb中設置自定義會話控制器:

devise_for :users, path: 'auth', skip: [:sessions],
           controllers: {
               sessions: 'users/sessions'
           }

as :user do
  get 'auth/sign_in' => 'home#index', as: :new_user_session
  post 'auth/sign_in' => 'users/sessions#create', as: :user_session
  delete 'auth/sign_out' => 'users/sessions#destroy', as: :destroy_user_session
end



Related

ruby-on-rails ruby-on-rails-3 devise