代码之家  ›  专栏  ›  技术社区  ›  Jay

Rails:在验证中使用与模型无关的表单字段

  •  10
  • Jay  · 技术社区  · 16 年前

    在RubyonRails应用程序中,我试图使用来自与验证中的模型不相关的字段的信息。

    下面是模型的一部分作为例子(整个模型已经变得有点大):

    class Scorecard < ActiveRecord::Base
      belongs_to :course
      belongs_to :user
    
      validate :attributes_consistency
    
      def attributes_consistency
        # Executed for all scorecards.  Checks if the user completed the hole attributes correctly
        if ( params[:no_fairways] and any_fairways? and !only_nine? ) or ( params[:no_fairways] and !any_h1_to_h9_score_blank and any_h1_to_h9_fairway? and only_nine? ) or ( params[:no_fairways] and !any_h10_to_h18_score_blank and any_h10_to_h18_fairway? and only_nine? )
          errors.add_to_base("You inidicated that you missed all the fairways, but you also marked one or more fairways in the scorecard.  Either uncheck the fairways mistakenly marked or uncheck the 'No fairways' checkbox.")
        end
        if ( params[:no_girs] and any_girs? and !only_nine? ) or ( params[:no_girs] and !any_h1_to_h9_score_blank and any_h1_to_h9_gir? and only_nine? ) or ( params[:no_girs] and !any_h10_to_h18_score_blank and any_h10_to_h18_gir? and only_nine? )
          errors.add_to_base("You inidicated that you missed all the greens, but you also marked one or more greens in the scorecard.  Either uncheck the marked greens on the scorecard or uncheck the 'No GIRs' checkbox.")
        end
      end # attributes_consistency
    
    
      def any_h1_to_h9_score_blank?
        h1_score.blank? or h2_score.blank? or h3_score.blank? or h4_score.blank? or h5_score.blank? or h6_score.blank? or h7_score.blank? or h8_score.blank? or h9_score.blank?
      end
      def any_h10_to_h18_score_blank?
        h10_score.blank? or h11_score.blank? or h12_score.blank? or h13_score.blank? or h14_score.blank? or h15_score.blank? or h16_score.blank? or h17_score.blank? or h18_score.blank?
      end
    
      def any_h1_to_h9_fairway?
        h1_fairway? or h2_fairway? or h3_fairway? or h4_fairway? or h5_fairway? or h6_fairway? or h7_fairway? or h8_fairway? or h9_fairway?
      end
      def any_h10_to_h18_fairway?
        h10_fairway? or h11_fairway? or h12_fairway? or h13_fairway? or h14_fairway? or h15_fairway? or h16_fairway? or h17_fairway? or h18_fairway?
      end
    
      def any_h1_to_h9_gir?
        h1_gir? or h2_gir? or h3_gir? or h4_gir? or h5_gir? or h6_gir? or h7_gir? or h8_gir? or h9_gir?
      end
      def any_h10_to_h18_gir?
        h10_gir? or h11_gir? or h12_gir? or h13_gir? or h14_gir? or h15_gir? or h16_gir? or h17_gir? or h18_gir?
      end
    

    那么我怎么才能进入 params 从模型?

    2 回复  |  直到 16 年前
        1
  •  15
  •   Anurag    16 年前

    别让人偷偷摸摸地接近模特儿。在这种情况下没有控制器的意义。相反,从 Railscasts 这涉及到不进入数据库但仍可以用于验证的虚拟属性。

    虚拟属性不需要对应的模型属性。定义类的局部属性,例如 @no_fairways 它控制着国家。

    class ScoreCard < ActiveRecord::Base
      # define attributes and accessors for both fields
      attr_accessor :no_fairways, :no_girs
    
      ..
    end
    

    现在,在您的表单中,您可以只写:

    <% form_for @scorecard %>
      <%= f.check_box :no_fairways %>
    <% end %>
    
        2
  •  3
  •   Jay    16 年前

    找到了解决方案,不过感谢这个术语,“虚拟属性”帮助了谷歌搜索。

    实现这一点的最干净的方法是创建属性,这些属性不是数据库的一部分,而是模型的一部分。在我的例子中,我把这个放到模型中:

    attr_accessor :no_fairways
    attr_accessor :no_girs
    

    那太简单了!现在 @scorecard.no_fairways 和 @scorecard.no_girs 与其他属性一样,但不属于数据库的一部分。

    推荐文章