开发者

rails redirect to the template of superclass if template is missing for some subclass

I am using Single-Table-Inheritance for a rails project. And I was wondering if it were possible to share functionality for some common functions between subclasses by implementing them in the superclass.

Is it possible to recycle the views of the superclass as well? Or should I write new views for the common functions? This wouldn't be very DRY.开发者_如何学编程 What were the DRY approach? Should I edit the routes.rb or is there another way to dynamically accomplish this?

Best, E.


First, you can definitely share functionality between subclasses by implementing at the superclass level - that's one of the big draws for STI.

As for the view question: I assume we are talking about subclassing a model, not a controller. In that case, a single controller (with normal views) for the main model will usually work fine for all of the subclasses. There are some slightly tricky issues when it comes to the forms, and creation/editing though. In particular, you will need to pull out the subclass of the model from the parameters and add it after updating everything else. For example:

def create
  citation_class = params[:citation].try(:delete, :type)
  @citation = citations.new(params[:citation])
  @citation.type = citation_class
  flash[:notice] = 'Citation was successfully created.' if @citation.save

  respond_with @citation
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜