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

时间:2022-03-13 23:26:41

In a Ruby on Rails application I am trying to use information from fields that are not associated with the model in validation.

在Ruby on Rails应用程序中,我尝试使用来自验证中与模型无关的字段的信息。

Here is part of the model as an example (the whole model has gotten kinda big):

这是模型的一部分作为一个例子(整个模型有点大):

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

So how can I access params from the model?

那么如何从模型中访问参数?

2 个解决方案

#1


15  

Don't let params sneak up to the model. There's no point of having a controller in that case. Instead, checkout this episode from Railscasts that talks about virtual attributes that do not go into the database but can still be used for validations.

不要让params潜入模特。在这种情况下,没有控制器。相反,请从Railscasts查看此剧集,其中讨论的虚拟属性不会进入数据库,但仍可用于验证。

You don't need a corresponding model attribute for the virtual attributes. Define attributes local to the class such as @no_fairways that hold the state.

您不需要虚拟属性的相应模型属性。定义类的本地属性,例如保存状态的@no_fairways。

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

  ..
end

Now inside you form, you could just write:

现在你在里面形成,你可以写:

<% form_for @scorecard %>
  <%= f.check_box :no_fairways %>
<% end %>

#2


3  

Found the solution, thanks for the lingo though, "virtual attribute" helped with the google searchin.

找到解决方案,感谢语言,“虚拟属性”帮助谷歌搜索。

The cleanliest way to accomplish this is to create attributes that are not part of the database but still part of the model. In my case I put this into the model:

实现此目的的最简洁方法是创建不属于数据库但仍属于模型的属性。在我的情况下,我把它放入模型:

attr_accessor :no_fairways
attr_accessor :no_girs

That easy! Now @scorecard.no_fairways and @scorecard.no_girs act just like any other attribute but aren't part of the database.

那简单!现在@ scorecard.no_fairways和@ scorecard.no_girs就像任何其他属性一样,但不是数据库的一部分。

#1


15  

Don't let params sneak up to the model. There's no point of having a controller in that case. Instead, checkout this episode from Railscasts that talks about virtual attributes that do not go into the database but can still be used for validations.

不要让params潜入模特。在这种情况下,没有控制器。相反,请从Railscasts查看此剧集,其中讨论的虚拟属性不会进入数据库,但仍可用于验证。

You don't need a corresponding model attribute for the virtual attributes. Define attributes local to the class such as @no_fairways that hold the state.

您不需要虚拟属性的相应模型属性。定义类的本地属性,例如保存状态的@no_fairways。

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

  ..
end

Now inside you form, you could just write:

现在你在里面形成,你可以写:

<% form_for @scorecard %>
  <%= f.check_box :no_fairways %>
<% end %>

#2


3  

Found the solution, thanks for the lingo though, "virtual attribute" helped with the google searchin.

找到解决方案,感谢语言,“虚拟属性”帮助谷歌搜索。

The cleanliest way to accomplish this is to create attributes that are not part of the database but still part of the model. In my case I put this into the model:

实现此目的的最简洁方法是创建不属于数据库但仍属于模型的属性。在我的情况下,我把它放入模型:

attr_accessor :no_fairways
attr_accessor :no_girs

That easy! Now @scorecard.no_fairways and @scorecard.no_girs act just like any other attribute but aren't part of the database.

那简单!现在@ scorecard.no_fairways和@ scorecard.no_girs就像任何其他属性一样,但不是数据库的一部分。