2014-03-07 93 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,因爲我不知道你是如何設置此爲您的查看代碼不是很清楚。