如何添加动态重定向?我试过了,但ApplicationController没有ApplicationHelper成功。
我想要这样的东西:
def dynamic_path
if current_user.admin?
admin_path
else
overview_path
end
end
最好的做法是什么?
谢谢!
编辑:
哦,我忘了提到我想把它放到我的 gem 中,它被两个不同的应用程序使用,并且都应该使用这个方法。我应该把它放在哪里?
如何添加动态重定向?我试过了,但ApplicationController没有ApplicationHelper成功。
我想要这样的东西:
def dynamic_path
if current_user.admin?
admin_path
else
overview_path
end
end
最好的做法是什么?
谢谢!
编辑:
哦,我忘了提到我想把它放到我的 gem 中,它被两个不同的应用程序使用,并且都应该使用这个方法。我应该把它放在哪里?
尝试将其放入ApplicationController,然后helper_method在应用程序控制器的顶部添加一行,如下所示:
helper_method :dynamic_path
def dynamic_path
redirect_to (current_user.admin? ? admin_path : overview_path)
end
该helper_method行使此方法在您的所有视图和控制器中都可用。
使用redirect_to:
def dynamic_path
redirect_to (current_user.admin? ? admin_path : overview_path)
end
更新:因为听起来您正在尝试将此帮助模块存储在外部 gem 中,您需要确保您的模块作为 ActionView 帮助程序加载,这可以通过在您的 gem 中使用Railtie自动完成。看