验证Rails中不在模型/控制器中的字段

时间:2021-01-28 11:51:37

I'm a relative newbie at Rails, so forgive me if my question's got an obvious answer.

我是Rails的相对新手,如果我的问题有明显的答案,请原谅我。

I'm trying to include a field in a Rails form which isn't in the model/controller or the migration associated with the view.

我试图在Rails表单中包含一个字段,该表单不在模型/控制器中或与视图关联的迁移中。

The form is a simple public contact form, and I can validate against most of the fields easily enough. Eg name, email etc.

表单是一个简单的公共联系表单,我可以很容易地对大多数字段进行验证。例如姓名,电子邮件等

The model is form_submission.rb

该模型是form_submission.rb

However, I have a field in the contact form - captcha - which isn't mirrored in the form_submissions db table, etc.

但是,我在联系表单中有一个字段 - captcha - 它没有在form_submissions db表中镜像,等等。

There is a separate table, model etc for captcha which is captcha_answer.rb (etc)

验证码有一个单独的表,模型等,它是captcha_answer.rb(等)

The attributes for captcha_answer in the migration are: answer and is_correct.

迁移中captcha_answer的属性是:answer和is_correct。

The table simple contains a list of answers to a predefined question, some of which are true and some which are false.

表格简单包含预定义问题的答案列表,其中一些是真的,一些是假的。

Eg, the captcha question might be:

例如,验证码问题可能是:

Which is these is an animal?

这些是动物?

With the options of: cat, dog, tree, rabbit .. in a select.

有选项:猫,狗,树,兔..在一个选择。

What I want to be able to do is to validate that:

我想要做的是验证:

a) The captcha field exists in the POST (return message of "no captcha given" if not) b) The answer given has a value in captcha_answers.is_correct of true (return message of "you gave a wrong answer" if not)

a)验证码字段存在于POST中(如果没有,则返回“no captcha given”的消息)b)给出的答案在captcha_answers.is_correct中的值为true(如果没有,则返回“你给错了答案”的消息)

The capcha_answers.answer is always unique, so I want to do the equivalent of a SQL query which gets the first record where captcha_answers.answer = and returns the value of captcha_answers.is_correct

capcha_answers.answer总是唯一的,所以我想做一个SQL查询,它获取第一条记录,其中captcha_answers.answer =并返回captcha_answers.is_correct的值

Like I say, if the attribute was in form_submissions then I'd be able to validate it no problem, but I can't figure out how I can validate a field against something in another model.

就像我说的,如果属性在form_submissions中,那么我将能够验证它没有问题,但我无法弄清楚如何验证字段对另一个模型中的某些东西。

Any ideas?

2 个解决方案

#1


1  

Define accessors for the extra field and use usual ActiveRecord validations:

为额外字段定义访问器并使用通常的ActiveRecord验证:

class MyModel < ActiveRecord::Base
  attr_accessor :extra_field

  validates :extra_field, :presence => true

  validate :custom_validation_method

  def custom_validation_method
    errors.add :extra_field, :invalid unless extra_field == "correct"
  end
end

#2


2  

You can just add for example a hidden field and catch it in the controller:

您可以添加例如隐藏字段并在控制器中捕获它:

in your form:

在你的形式:

<%= hidden_field(:signup, :pass_confirm, :value => 'abcd') %>

then in the controller:

然后在控制器中:

params[:signup]

There you can access a different model and validate the answer.

在那里,您可以访问不同的模型并验证答案。

Action in the controller like:

控制器中的动作如:

def update
    @company = Company.find(params[:id])

    puts "extra field:" 
    puts params[:signup]

    respond_to do |format|
      if @company.update_attributes(params[:company])
        format.html { redirect_to @company, :notice => 'Company was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @company.errors, :status => :unprocessable_entity }
      end
    end
  end

#1


1  

Define accessors for the extra field and use usual ActiveRecord validations:

为额外字段定义访问器并使用通常的ActiveRecord验证:

class MyModel < ActiveRecord::Base
  attr_accessor :extra_field

  validates :extra_field, :presence => true

  validate :custom_validation_method

  def custom_validation_method
    errors.add :extra_field, :invalid unless extra_field == "correct"
  end
end

#2


2  

You can just add for example a hidden field and catch it in the controller:

您可以添加例如隐藏字段并在控制器中捕获它:

in your form:

在你的形式:

<%= hidden_field(:signup, :pass_confirm, :value => 'abcd') %>

then in the controller:

然后在控制器中:

params[:signup]

There you can access a different model and validate the answer.

在那里,您可以访问不同的模型并验证答案。

Action in the controller like:

控制器中的动作如:

def update
    @company = Company.find(params[:id])

    puts "extra field:" 
    puts params[:signup]

    respond_to do |format|
      if @company.update_attributes(params[:company])
        format.html { redirect_to @company, :notice => 'Company was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @company.errors, :status => :unprocessable_entity }
      end
    end
  end