2011-03-30 76 views
0

我有一个使用remote_form_for标记提交的表单。使用RJS Rails进行表单验证

查看

<% form_remote_tag(:url => { :action => 'create' }) do %> 

a bunch of fields 

<% end %> 

控制器

def create 

    if @greeting.can_save? 
    respond_to do |format| 
    format.html {redirect_to board_link(@board)} 
    format.js #close the iframe and reload the parent 
    end 
    else 
    respond_to do |format| 
    format.html {redirect_to :action => 'new'} 
    format.js #show the errors on the form 
    end 
    end 
end 

create.rjs

page << "parent.$.fancybox.close();" 

所有形式被用正确的信息提交工作正常。但我需要为无效提交显示错误消息。

如何在窗体未通过验证时显示窗体上的错误消息?

在此先感谢。

回答

0

将表单拉出到它自己的部分中,在页面中呈现该部分,为表单提供唯一的ID。

在表单标签中,您应该以任何希望的方式呈现错误,无论是列出错误还是将类添加到受影响的字段。

现在做这样的事情在你的RJS如下:

if @greeting.valid? 
    page << "parent.$.fancybox.close();" 

else 
    page.replace 'my_form_id', :partial => 'greetings/form' 
end 

这将重新呈现形式,如果有一个错误。确保你的@greeting实例变量被传递给这个表单,以便字段被正确填充。

+0

非常感谢道格拉斯我会给它一个机会,看看它是怎么回事。 – chell 2011-03-30 12:23:28