2014-03-07 92 views
0

我有一些用户类型,如学生,教师,管理员。对于每个用户类型,我都有一个控制器。例如学生控制器放置在app/controllers/students目录中。但大多数控制器是相同的。因此我创建了核心控制器放置在app/controllers/coreRails从CoreController嵌套。路由问题

应用程序/控制器/核心/ settings_controller.rb

class Core::SettingsController < ApplicationController 

    def show 
    # instance variables, redirections etc. placed here... 
    # 
    end 

    # other actions... 
    # 
end 

应用程序/控制器/学生/ settings_controller.rb

class Student::SettingsController < Core::SettingsController 

end 

的routes.rb

namespace :student do 
    # ... 
    resource :schedule, :only => [:show] 
    resource :settings, :only => [:show, :update] 
end 

namespace :admin do 
    # ... 
    resource :settings, :only => [:show, :update] 
end 

当我将<%= link_to "<", schedule_path(:date => @date.prev_month)" %>添加到我的views/core/settings/_month_nav.html.erb文件时,问题就开始了。

是否有除了编写代码这个丑陋的一块块的任何解决方案:

<% if admin? %> 
    <%= link_to "<", admin_schedule_path ... %> 
<% elsif student? %> 
    <%= link_to "<", student_schedule_path ... %> 
... 
+3

听起来好像你没有使用STI - 如果没有,那么你可以认为它是一个选项,因为它看起来是为你的需求量身定制的,并且可能允许你拥有一个控制器。 [这是关于STI的Rails 4教程](http://thibaultdenizet.com/tutorial/single-table-inheritance-with-rails-4-part-1/)。 – omnikron

回答

2

你可以做类似

<%= link_to "<", self.send("#{current_user.class.to_s.downcase}_schedule_path",:date => @date.prev_month) %> 

我以前current_user,因为我不知道你是如何设置此为您的查看代码不是很清楚。