简体   繁体   English

Devise登录到根路由rails 3

[英]Devise logged in root route rails 3

Heyya guys . Heyya 家伙 So i thought about this coolio idea, if you are logged in then you get some sort of dashboard, else you get an information/login/sign up page.. So how do i do that.. 所以我想到了这个coolio想法,如果您登录了,那么您会获得某种仪表板,否则您将获得一个信息/登录/注册页面..那么我该怎么做。

I mostly wants to do this in Routes = not something like 我最想在Routes中做到这一点=并非如此


def index
  if current_user.present?
    render :action => 'logged_in'
  else
    render :action => 'logged_out'
  end
end

thanks in advance! 提前致谢!

/ Oluf Nielsen / Oluf Nielsen

Think you may have been looking for this: 认为您可能一直在寻找这个:

authenticated :user do
  root :to => "dashboard#show"
end

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

Note: it's authenticate*d* 注意:它是认证* d *

I too wanted this in my app, here's what I came up with. 我也想在我的应用程序中使用它,这就是我想出的。

MyCoolioApp::Application.routes.draw do
  root :to => 'users#dashboard', :constraints => lambda {|r| r.env["warden"].authenticate? }
  root :to => 'welcome#index'

  get "/" => 'users#dashboard', :as => "user_root"

  # ..
end

In Rails 3 you can use Request Based Contraints to dynamically map your root route. 在Rails 3中,您可以使用“ 基于请求的约束”来动态映射您的root路由。 The solution above works for the Devise authentication gem but can be modified to support your own implementation. 上面的解决方案适用于Devise身份验证gem,但可以修改以支持您自己的实现。

With the above root_path or / will route to a WelcomeController#index action for un-authenticated requests. 使用上面的root_path/将路由到未经验证的请求的WelcomeController#index操作。 When a user is logged in the same root_path will route to UsersController#dashboard . 当用户登录时,相同的root_path将路由到UsersController#dashboard

Hope this helps. 希望这可以帮助。

I have the same problem and I solved it with this: 我有同样的问题,我用以下方法解决了:

authenticated :user do
  root :to => "wathever#index"
end
unauthenticated :user do
  devise_scope :user do 
    get "/" => "devise/sessions#new"
  end
end

Hope it helps. 希望能帮助到你。

are you using devise's before filters? 您是否在使用devise的before过滤器?

class FooController < ActionController::Base
  before_filter :authenticate_user!
...

Why don't you try altering the default login views so they have the info/login/signup infos you want. 您为什么不尝试更改默认登录视图,以便它们具有所需的信息/登录/注册信息。

Here's what I'm using in my application layout file right now. 这就是我现在在应用程序布局文件中使用的东西。 Haven't broken it out into partials yet: 尚未将其分解为部分:

            <% if user_signed_in? %>
                <a href="/profile"><%= current_user.email %></a> | 
                <%= link_to "Logout", destroy_user_session_path %>
            <% else %>
                <%= link_to "Login", new_user_session_path %> |
                <%= link_to "Register", new_user_registration_path %>
            <% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM