私の環境
Rails 3.2.1
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
コミット値に応じて検証方法を変更したい。
たとえば、次のビューでは、「only_foo」がプッシュされたときに「foo」の検証のみをチェックしたいのですが、「only_bar」がプッシュされたときに「bar」の検証のみをチェックしたいのです。
<%= from_for(@user, :url => "/hoge/") %>
<%= f.text_field 'foo' %>
<%= f.text_field 'bar' %>
<%= f.submit 'only_foo' %>
<%= f.submit 'only_bar' %>
<% end %>
ユーザーモデルでは、validateメソッドでコミット値を取得したいと思います。
validate :foo, :presence => true, :if => :only_foo?
validate :bar, :presence => true, :if => :only_bar?
def only_foo?
# I want to get the commit value, like this.
commit == 'only_foo'
end
def only_bar?
# I want to get the commit value, like this.
commit == 'only_bar'
end
それとももっと良い練習がありますか?
よろしくお願いします。