我想驗證一個表單模型中的值(一個複選框實際上),但我有很多的麻煩找什麼傳遞只會驗證:如何驗證一個表單的值的Rails 3
validates :agreement, :agreement => true
我已經得到了其他事情一樣工作:
validates :password, :presence => true, :length => {:minimum => 6, :maximum => 25}, :confirmation => true
我的看法是這樣的:
<% form_for :signup_form, :url => {:controller => "user", :action => "post_signup"} do |f| %>
... <%= f.check_box(:agreement)%>我同意<%= link_to(「服務條款」,:controller =>「about」,:action =>「terms」)%>和< %=的link_to( 「隱私政策」,:控制器=> 「約」:動作=> 「隱私」)%> ...
然後進入我的控制器:
agreement = params[:signup_form][:agreement]
new_user = User.create(:login_name => login_name, :first_name => first_name, :last_name => last_name, :email => email, :password => password, :agreement => agreement, :created_at => DateTime.now())
然後我的模型。
感謝您提前提供任何幫助。
謝謝!以一種很好的方式顯示錯誤也是我一直在嘗試做的事情。在我剛剛檢查了控制器中明顯不正確的輸入並將閃存變量設置爲我想要顯示的內容之前:flash [:signup_error] =「找到了不完整的表單,請重試」 redirect_to(:controller =>「user」, :action =>「註冊」) – tarabyte 2011-03-29 18:36:27
不錯,我沒有意識到你可以通過這種方式進行驗證。我使用了validate:method_name – kvirani 2012-05-15 13:12:08