Rails表单验证条件旁路 [英] Rails form validation conditional bypass
问题描述
我有一个Rails模型,可以验证2个表单值的唯一性.如果这两个值不是唯一的,则会显示验证错误,并且提交"按钮将更改为重新提交".我想允许用户单击重新提交"按钮并绕过模型验证.我想从Rails验证文档中执行以下操作:
I have a rails model that validates uniqueness of 2 form values. If these 2 values aren't unique the validation errors are shows and the "submit" button is changed to "resubmit". I want to allow a user to click the "resubmit" button and bypass the model validation. I want to do something like this from the rails validation documentation:
validates_uniqueness_of :value, :unless => Proc.new { |user| user.signup_step <= 2 }
但是我的模型中没有可以检查的值...只是具有重新提交"值的参数.
but I don't have a a value in my model that I can check for...just the params that have the "Resubmit" value.
关于如何执行此操作的任何想法?
Any ideas on how to do this?
推荐答案
我认为这是最好的方法:
In my opinion this is the best way to do it:
class FooBar < ActiveRecord::Base
validates_uniqueness_of :foo, :bar, :unless => :force_submit
attr_accessor :force_submit
end
然后在您的视图中,确保将命名标签命名为
then in your view, make sure you name the submit tag like
<%= submit_tag 'Resubmit', :name => 'foo_bar[force_submit]' %>
这样,所有逻辑都在模型中,控制器代码将保持不变.
this way, all the logic is in the model, controller code will stay the same.
这篇关于Rails表单验证条件旁路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!