相同的命名空间,但不同的控制器和视图在Rails中
问题描述:
我有一个案例,当我需要localhost:3000/dashboard
指向基于用户类型的不同视图/控制器组合。我的申请中有两种主要类型:Subscriber
和Publisher
。相同的命名空间,但不同的控制器和视图在Rails中
当Publisher
登录并进入/dashboard
我需要显示发布仪表板。
当Subscriber
登录并转到/dashboard
我需要显示订购者仪表板。
此时发布商的仪表板被称为Dashboard
,订户的仪表板被称为Profile
。似乎对我有点肮脏。
问题是。根据特定用户的类型调用正确的控制器,加载正确的数据并呈现正确的模板/布局的最佳方式是什么?
答
我会考虑像下面的伪代码,让你开始。
控制器:
app/controllers/dashboard_controller.rb
class Dashboard < ApplicationController
def index
render, :user_type => current_user.user_type
end
查看: (使用助手改一下会显示)。
views/dashboards/index.html.erb
# display the content
帮手。
helpers/dashboard_helper.rb
module DashboardHelper(user_type)
if user_type == 'publisher'
#set content/variables for publisher
elsif user_type == 'Subscriber'
#set content/variables for subscriber
else
set content/variables to default.
end
谢谢迈克尔!但是,我想实施它的方式与您的建议不同,我确实遵循了您的建议。代码如下所示: 'def index' '@publisher = @subscriber = current_user' 'render template:“#{current_user.type.underscore.pluralize}/dashboard”' 'end' – Ivan 2012-04-21 12:10:35