2010-07-24 18 views
10

在Ruby on Rails應用程序中,我嘗試使用與驗證中與模型無關的字段中的信息。Rails:在驗證中使用與模型無關的表單字段

這裏是模型爲例的一部分(整個模型已經變得有點大):

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

回答

15

不要讓params潛入模型。在這種情況下有一個控制器是沒有意義的。相反,請結合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 %> 
+0

這將是一個很好的解決方案,但你可以給我一個例子,我可能會實現這個?在那個特定的Railscast中,「虛擬屬性」實際上是操縱真實屬性的方法。我沒有'no_fairways'或'no_girs'可以關聯的屬性。 – Jay 2010-07-24 23:25:14

+0

@James - 虛擬屬性不必與任何數據庫屬性相關聯。你可以定義實例屬性,它也可以工作。更新了答案。 – Anurag 2010-07-24 23:59:48

+0

請注意,與複選框關聯的attr不會具有trythy或falsy值。默認情況下,'check_box'生成標記,在未選中時產生'0'字符串,並在選中時產生'1'。即使'「0」.present?'是真的。 – kolen 2018-03-09 18:02:36

3

找到了解決辦法,感謝您的行話不過,「虛擬屬性」幫助與谷歌Searchin的。

完成此操作的最簡單方法是創建不屬於數據庫但屬於模型一部分的屬性。在我的情況下,我把這個模型:

attr_accessor :no_fairways 
attr_accessor :no_girs 

這很容易!現在,@scorecard.no_fairways@scorecard.no_girs的行爲與其他任何屬性一樣,但不屬於數據庫的一部分。