【问题标题】:rails redirect to the template of superclass if template is missing for some subclass如果某些子类缺少模板,rails 将重定向到超类的模板
【发布时间】:2011-05-11 09:32:28
【问题描述】:

我在 Rails 项目中使用单表继承。而且我想知道是否可以通过在超类中实现它们来在子类之间共享一些通用功能的功能。

是否也可以回收超类的视图?还是应该为常用功能编写新视图?这不会很干燥。 DRY 方法是什么?我应该编辑routes.rb 还是有其他方法可以动态地完成这个?

最好, E.

【问题讨论】:

    标签: ruby-on-rails model-view-controller inheritance dry single-table-inheritance


    【解决方案1】:

    首先,您绝对可以通过在超类级别实现来在子类之间共享功能 - 这是 STI 的一大吸引力。

    至于视图问题:我假设我们谈论的是子类化模型,而不是控制器。在这种情况下,主模型的单个控制器(具有普通视图)通常对所有子类都可以正常工作。在表单和创建/编辑方面存在一些稍微棘手的问题。特别是,您需要从参数中提取模型的子类,并在更新其他所有内容后添加它。例如:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 2016-04-26
      • 2015-09-08
      相关资源
      最近更新 更多