Rus*_*nov 1 controller ruby-on-rails
我有一个Post模特.的Post可能是一个问题类型,应答或注释(类似于StackOverflow的结构).目前,控制器正在处理任何类型的CRUD请求 Post.有一个约定,控制器需要在Rails中很薄,而且我的控制器似乎不遵循这个,因为Post任何操作中的case语句(类型选择和处理).所以我的问题是:有没有办法重组或(甚至更好)划分Post控制器来处理Question,Answer并Comment单独使用,但使用共同的Post观点?任何链接/示例将不胜感激.
您可以从另一个继承一个控制器:
一个常用控制器:
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
Run Code Online (Sandbox Code Playgroud)
然后在每个控制器中:
class AnswersController < PostsController
self.model_class = Post
# here all specific methods
def create
...
end
end
Run Code Online (Sandbox Code Playgroud)
您可以保留在文件夹中的所有视图posts,或者与其自己的文件夹不同的视图