[英]devise not redirect after sign in
我是Rails世界的新手,我需要一點幫助,我運行devise gem並運行完美,登錄后我只是遇到了一個小問題,我沒有想要的頁面,並且總是重定向到我的索引頁面,可以嗎伙計們給些幫助嗎?
關於視圖:我有用於儀表板和索引的索引文件。
有我的代碼:
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :dashboard
root to: "home#index"
dashboard_controller.rb
class DashboardController < ApplicationController
before_filter :authenticate_user!
def index
end
end
home_controller.rb
class HomeController < ApplicationController
def index
if user_signed_in?
redirect_to :controller=>'dashboard', :action => 'index'
end
end
end
aplication.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Devise</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="user_nav">
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. Not you?
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or <%= link_to "sign in", new_user_session_path %>
<% end %>
</div>
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<%= yield %>
</body>
</html>
編輯:找到解決方法:在aplication_controller.rb中添加
def after_sign_in_path_for(resource)
dashboard_index_path
end
謝謝你,一切順利
在您的應用程序控制器中,添加以下方法:
def after_sign_in_path_for(resource)
dashboards_path #edited
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
def after_sign_in_path_for(resource)
dashboard_index_path
end
end
class DashboardController < ApplicationController
**end**
before_filter :authenticate_user!
def index
end
為什么儀表板控制器和家庭控制器中的end_filter在end之前,請刪除該文件並將其添加到文件末尾。
其次,使用redirect_to dashbaord_path
而不是redirect_to :controller=>'dashboard', :action => 'index'
試試這個
redirect_to url_for(:controller => :dashboard, :action => :index)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.