我有一个案例,我需要 localhost:3000/dashboard
来根据用户类型指向不同的 View / Controller 组合。我的应用程序中的两个主要类型是 Subscriber
和 Publisher
。
当 Publisher
登录并转到 /dashboard
时,我需要显示 Publisher Dashboard。
当 Subscriber
登录并转到 /dashboard
时,我需要显示 Subscriber Dashboard。
此时,发布者的仪表板称为Dashboard
,订阅者的仪表板称为Profile
。在我看来有点脏。
问题是。根据特定用户的类型调用正确的 Controller 、加载正确的数据并呈现正确的模板/布局的最佳方式是什么?
最佳答案
我会考虑像下面的伪代码这样的东西来帮助你入门。
Controller :
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
关于ruby-on-rails - Rails 中相同的命名空间但不同的 Controller 和 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10249391/