2011-02-14 160 views
17

设计给我一个相当困难的时间。目前,除了注册重定向之外,其他一切似乎都在起作用。我想设计重定向到我的镇控制器在索引行动,注册或登录(登录实际工作)。注册后重新设计

我试着重写RegistrationsController,我已经尝试添加一个applicationsController功能,如:

def after_sign_in_path_for(resource_or_scope) 
    if resource_or_scope.is_a?(User) 
     town_path 
    else 
     super 
    end 
    end 

不过,我得到了同样的错误:

NoMethodError in User/townController#index 

You have a nil object when you didn't expect it! 
You might have expected an instance of Array. 
The error occurred while evaluating nil.* 

认真,我无法找到一种方法来做到这一点。请有任何想法吗? :)

编辑:MY ROUTES

 new_user_session GET /users/sign_in(.:format)      {:action=>"new", :controller=>"devise/sessions"} 
      user_session POST /users/sign_in(.:format)      {:action=>"create", :controller=>"devise/sessions"} 
    destroy_user_session GET /users/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"} 
     user_password POST /users/password(.:format)      {:action=>"create", :controller=>"devise/passwords"} 
    new_user_password GET /users/password/new(.:format)     {:action=>"new", :controller=>"devise/passwords"} 
    edit_user_password GET /users/password/edit(.:format)     {:action=>"edit", :controller=>"devise/passwords"} 
         PUT /users/password(.:format)      {:action=>"update", :controller=>"devise/passwords"} 
    user_registration POST /users(.:format)        {:action=>"create", :controller=>"devise/registrations"} 
new_user_registration GET /users/sign_up(.:format)      {:action=>"new", :controller=>"devise/registrations"} 
edit_user_registration GET /users/edit(.:format)       {:action=>"edit", :controller=>"devise/registrations"} 
         PUT /users(.:format)        {:action=>"update", :controller=>"devise/registrations"} 
         DELETE /users(.:format)        {:action=>"destroy", :controller=>"devise/registrations"} 
        root  /(.:format)         {:action=>"index", :controller=>"home"} 
      user_root  /user(.:format)        {:action=>"index", :controller=>"user/town"} 
        home  /home(.:format)        {:action=>"index", :controller=>"home"} 
        town  /town(.:format)        {:action=>"index", :controller=>"town"} 
       inbox  /messages(.:format)       {:action=>"index", :controller=>"messages"} 
       inbox  /messages/inbox(.:format)      {:action=>"inbox", :controller=>"messages"} 

的routes.rb:

devise_for :users 

    root :to => "home#index" 

    namespace :user do 
    root :to => "town#index" 
    end 

    scope :path => '/home', :controller => :home do 
    match '/' => :index, :as => 'home' 
    end 

    scope :path => '/town', :controller => :town do 
    match '/' => :index, :as => 'town' 
    end 
...... 
+0

显示我们通过`$耙routes`请 – raidfive 2011-02-14 00:17:17

+0

感谢名单的路线,我添加了相关的路线 – Spyros 2011-02-14 00:23:27

+1

向我们展示你的routes.rb内的色器件指令。从外观来看,资源不是用户。找出它实际上是哪个类。 – Dex 2011-02-14 01:33:40

回答

11

这就是我如何得到这个工作。

# In your routes.rb 
match 'dashboard' => 'user_dashboard#index', :as => 'user_root' 

然后确保你没有你的home#index控制器上的before_filter :authenticate_user!集。

6

因为没有人接,我会附和你确定这是这是造成错误的代码?你的resource_or_scope为零使得它看起来像设计是问题,但我怀疑是这样。所以我认为问题在别的地方。

我想尝试一下以确保它首先工作。

def after_sign_in_path_for(resource) 
    "http://www.google.com" 
    end 

如果这样做工作,然后尝试检查resource变量,以确保它不是零。

2

您是否尝试过重写设计sign_in_and_redirect方法;它会为你工作

def sign_in_and_redirect(resource_or_scope,resource) 
    if resource_or_scope == :user 
     redirect_to town_path 
    else 
     super 
    end 
end 
0

我有类似的问题。最终诉诸于应用程序控制器中的if语句。如果用户登录,他们没有配置文件,然后路由到new_profile_path

1

不知道这是否有帮助,但我遇到了上述相同的问题,但区别是我使用自定义注册控制器。

从RegistrationsController的设计源代码看来它调用了sign_in_and_redirect,然后它将它传递给redirect_location方法。

我重写redirect_location以仅返回来自after_sign_up_path_for(resource)而不是stored_location_for(scope)的路径。 after_sign_up_path_for(资源)。

在after_sign_up_path_for(资源),我自定义的方法来重定向到基于

作品中的Rails 3.0.5和1.3.0制定对我的用户类型正确的道路。

14

在Ruby On Rails 3.0.7中使用Devise 1.3.4。检查出一个解决方案互联网后,我所做的就是简单地粘贴下面的代码

* 第一) *为一个成功的标志了欢迎页面重定向后,发生在/config/routes.rb以下(注意,你提供给devise_for参数替换:user):

namespace :user do 
root :to => "welcome#index" 
end 

*秒) *要在退出后重定向(欢迎页面也),地点在/ app /控制器/ application_controller .rb此方法:

private 
# Overwriting the sign_out redirect path method 
def after_sign_out_path_for(resource_or_scope) 
welcome_index_path 
end 

这对我有用,我希望它适合所有人。

1

的问题是,设计登录到刚刚创建的资源时绕过after_sign_in_path_for(resource)。你需要的是after_sign_up_path_for(resource)

# override in your ApplicationController or custom Devise::RegistrationsController 
def after_sign_up_path_for(resource) 
    if resource.is_a?(User) 
    town_path 
    else 
    super 
    end 
end 

您仍然需要你的after_sign_in_path_for(resource)覆盖到后续登录插件正确地重定向到资源。

1

托尼说了什么。命名空间并不意味着以这种方式使用。我不知道是谁发明了这种根本不起作用的黑客,但不幸的是,这是Google搜索时出现的第一个解决方案之一。

namespace :user do 
    root :to => "town#index" 
end 

套“/用户”为user_root_path并指出“用户/镇”控制器的index动作,你没有。你有什么是'镇'控制器。这就是你得到错误的原因。你需要的是

get '/town', to: 'town#index', as: 'user_root' 

这样,“/镇”指向“镇#指数”,并设置为你的user_root_path,这是设计重定向到用户后

  1. 登录
  2. 注册
  3. 更新密码

,所以你甚至都不需要重写after_sign_in_path_for,其中,反之为w许多人认为,这不是解决设计重定向问题的最佳地点。

on Rails的4.1+和设计3.2