Rails3 Ajax表单(jquery)和模型验证

时间:2022-10-08 23:39:51

i send an form remotely and wanted to add a validation to it. this is in my model:

我远程发送表单,并希望为其添加验证。这是在我的模型中:

  validates :email, :presence => true, :email => true

validation works fine, when email is valid it adds it to the database, if not, then not. my problem is, whenever i submit the form, it renders my subscribe.js file:

验证工作正常,当电子邮件有效时,它会将其添加到数据库,如果没有,则不然。我的问题是,每当我提交表单时,它都会呈现我的subscribe.js文件:

$('.form').html("<p><strong>Thank you for your interest!<br />We'll keep you up-to-date.</strong></p>"); 

how can i tell rails, that if the email adress is unvalid, it should render another .js file? or is there another way?

我怎么能告诉rails,如果电子邮件地址无效,它应该呈现另一个.js文件?还是有另一种方式?

thank you!

谢谢!

1 个解决方案

#1


1  

Maybe something like this...

也许是这样的......

if @object.save 
  respond_to do |format|
    format.js
  end
else   
  render # something else
end

#1


1  

Maybe something like this...

也许是这样的......

if @object.save 
  respond_to do |format|
    format.js
  end
else   
  render # something else
end