Rails 设计:两个不同 after_update_path_for
Rails Devise: two different after_update_path_for
我有两个页面呈现 Devise 的更新用户表单。经典的 (users/edit) 和一个仅包含完整编辑表单的一部分的“/page”页面。我想有两个不同的 after_update_path 无论表单是提交到一个页面还是另一个页面。我尝试了几件事,但我 none 正在工作...
def after_update_path_for(resource)
if current_page?('/page')
:page
else
:root_path
end
end
我收到以下错误:
undefined method `current_page?' for #RegistrationsController:0x007fe9db304e28 Did you mean? current_user
知道是否可以这样做吗?
看起来您需要包含 ActionView::Helpers::UrlHelper
,因为该方法在该控制器中未定义。
class Users::RegistrationsController < Devise::SessionsController
include ActionView::Helpers::UrlHelper
def after_update_path_for(resource)
if current_page?( '/path' )
render :page
else
render :root_path
end
end
end
这是docs for the method。注意上面的评论:
The default url to be used after updating a resource. You need to overwrite this method in your own RegistrationsController.
如果出于某种原因这在 rails 3 中不起作用,您可以尝试这样做:
def after_update_path_for(resource)
if params[:action] == 'my_action' && params[:controller] == 'my_controller'
render :page
else
render :root_path
end
end
我有两个页面呈现 Devise 的更新用户表单。经典的 (users/edit) 和一个仅包含完整编辑表单的一部分的“/page”页面。我想有两个不同的 after_update_path 无论表单是提交到一个页面还是另一个页面。我尝试了几件事,但我 none 正在工作...
def after_update_path_for(resource)
if current_page?('/page')
:page
else
:root_path
end
end
我收到以下错误:
undefined method `current_page?' for #RegistrationsController:0x007fe9db304e28 Did you mean? current_user
知道是否可以这样做吗?
看起来您需要包含 ActionView::Helpers::UrlHelper
,因为该方法在该控制器中未定义。
class Users::RegistrationsController < Devise::SessionsController
include ActionView::Helpers::UrlHelper
def after_update_path_for(resource)
if current_page?( '/path' )
render :page
else
render :root_path
end
end
end
这是docs for the method。注意上面的评论:
The default url to be used after updating a resource. You need to overwrite this method in your own RegistrationsController.
如果出于某种原因这在 rails 3 中不起作用,您可以尝试这样做:
def after_update_path_for(resource)
if params[:action] == 'my_action' && params[:controller] == 'my_controller'
render :page
else
render :root_path
end
end