2012-03-26 106 views
1

我有一個Post模型。 Post可能是一種問題,答案或評論(類似於StackOverflow結構)。現在,控制器正在處理請求以刪除任何類型的Post。有一個約定,控制器在Rails中需要很細,而且我的控制器似乎沒有遵循這個規則,因爲在任何操作中都有case case語句(Post類型選擇和處理)。所以我的問題:有沒有辦法重新組織或(甚至更好)將Post控制器分別處理QuestionAnswerComment,但使用共同的Post意見?任何鏈接/示例將不勝感激。Rails一個模型 - 許多控制器

回答

5

您可以直接從另一個繼承一個控制器:

一個共同的控制器:

class PostsController < ApplicationController 

    #here all the methods common for all types, if any 
    def new 
    @post = Post.new(:email => current_user.try(:email)) 
    end 
    ... 

end 

然後在每個控制器:

class AnswersController < PostsController 

    self.model_class = Post 

    # here all specific methods 
    def create 
    ... 
    end 
end 

而且各方面的意見,你可以保留的文件夾中posts,或那些不同於自己的文件夾

+0

謝謝,acce解決方案 – 2012-03-27 08:15:10