Ruby on rails 注册后设计重定向
迪安给了我一段相当艰难的时光。目前,除了注册重定向之外,其他一切似乎都在运行。我想设计重定向到我的城市管理员在索引行动,在注册或登录(登录实际工作) 我已尝试覆盖RegistrationController,并尝试添加ApplicationController函数,如:Ruby on rails 注册后设计重定向,ruby-on-rails,Ruby On Rails,迪安给了我一段相当艰难的时光。目前,除了注册重定向之外,其他一切似乎都在运行。我想设计重定向到我的城市管理员在索引行动,在注册或登录(登录实际工作) 我已尝试覆盖RegistrationController,并尝试添加ApplicationController函数,如: def after_sign_in_path_for(resource_or_scope) if resource_or_scope.is_a?(User) town_path else
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.*
说真的,我找不到办法。有什么想法吗?:)
编辑:我的路线
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
......
既然没人接,我就插话。您确定这是导致错误的代码吗?你的
resource\u或\u scope
为零让人觉得设计是个问题,但我怀疑情况是否如此。所以我认为问题出在别的地方
我会先尝试下面的方法,以确保它能正常工作
def after_sign_in_path_for(resource)
"http://www.google.com"
end
如果有效,则尝试检查
资源变量以确保它不是零。您是否尝试覆盖designe登录和重定向方法;它会对你有用的
def sign_in_and_redirect(resource_or_scope,resource)
if resource_or_scope == :user
redirect_to town_path
else
super
end
end
这就是我工作的方式
# In your routes.rb
match 'dashboard' => 'user_dashboard#index', :as => 'user_root'
然后确保您在\u过滤器之前没有:验证\u用户在你的home#index
控制器上设置code>。我也遇到了类似的问题。最终求助于应用程序控制器中的if语句。如果用户登录并且没有配置文件,则路由到新的配置文件路径不确定这是否有用,但我遇到了上述相同的问题,但区别在于我使用的是自定义注册控制器
从RegistrationController的Desive源代码来看,它似乎在调用sign_in_和_redirect,然后将其传递给redirect_location方法
我重写重定向位置,只返回(资源)的注册后路径,而不是(范围)的存储位置,而不是(资源)的注册后路径
在为(资源)注册路径之后,我根据用户类型定制了重定向到正确路径的方法
适用于Rails 3.0.5和Desive 1.3.0。我使用Desive 1.3.4和Ruby On Rails 3.0.7。在查看internet上的解决方案后,我所做的只是粘贴以下代码
*首先)*要在成功注册到欢迎页面后重定向,请在/config/routes.rb中放置以下内容(注意,将:user
替换为您为设计的参数):
*第二个)*要在注销后重定向(也转到欢迎页面),请将此方法放置在/app/controllers/application_controller.rb中:
private
# Overwriting the sign_out redirect path method
def after_sign_out_path_for(resource_or_scope)
welcome_index_path
end
这对我很有效,我希望对你们所有人都有效。这个问题的答案对我非常有效,请在Desive Wiki上进行解释:
问题在于,当登录到刚创建的资源时,Deave在(资源)的路径中进行签名后绕过。您需要的是在为(资源)注册路径后:
在为(资源)登录路径后,您仍然需要使用覆盖来正确地将后续登录重定向到资源。Tony所说的。名称空间不应以这种方式使用。我不知道是谁想出了这个根本不起作用的方法,但不幸的是,这是谷歌搜索时最先想到的解决方案之一
namespace :user do
root :to => "town#index"
end
将“/user”设置为用户根路径,并指向您没有的“user/town”控制器的索引操作。你拥有的是“城镇”管制员。这就是你出错的原因。你需要的是
get '/town', to: 'town#index', as: 'user_root'
这样,“/town”指向“town#index”,并设置为您的用户_root_路径,这就是designe在用户之后重定向到的路径
登录
报名
更新密码
因此,您甚至不需要在
的路径中签名后覆盖,这与许多人认为的相反,并不是解决设计重定向问题的最佳方法
在Rails 4.1+和Desive 3.2上,通过$rake routes
pleaseThanx向我们展示您的路线,我在您的routes.rb中添加了相关的路线Show us your Desive指令。从外观上看,资源不是用户。弄清楚它实际上是哪个类。有趣的是,我在为(资源)的路径中签名之后,只有在我将它添加到application\u helper.rb而不是控制器中时,它才起作用。尽管文档中说要覆盖controller方法,但似乎您实际上需要覆盖applicationhelper方法,这就是所调用的方法。您好,我以前确实尝试过完全相同的代码,它只在登录时有效。但不是在注册后登录。在登录用户、确认帐户或更新密码后,Desive将从Desive wiki中查找要重定向的作用域根路径。示例:对于:user资源,如果存在,它将使用user\u root\u路径。我看到您的路由中有一个用户\u root
。这就是它结束的地方吗?我有一个名称空间:user do root:to=>“town#index”结尾,它在登录时确实以town/index结尾。但是,当一个新用户注册并登录(设计默认行为)时,我会收到上面的错误(仍然会重定向到town/index,但是有这个错误,似乎我没有当前的用户对象)。将其添加到我的自定义注册控制器,但仍然会收到相同的错误:/Desive 1.2.rc,在Desive Wiki中有一个更好的方法来实现这一点,你说得对。我们需要将您的编辑为带有引号的:'user\u root'
,并确保您不会在任何地方使用towns\u path
,否则您也需要更改它们,因为在应用程序中使用该助手时会出错。
namespace :user do
root :to => "town#index"
end
get '/town', to: 'town#index', as: 'user_root'