我有一个表格,我(使用原型和内置的轨道“form_remote_tag”助手)通过AJAX提交。
我想更新一个div(一个状态区),如果有表单验证错误,但是如果提交成功完成,则会有一个不同的div(表单所在的div)。更新不同的div
我的代码看起来是这样的:
<div id="recipe-status"><!-- I want form validation errors to go here --></div>
<div id="recipe">
<%= form_remote_tag(:update => "recipe-status",
:before => "Element.show('wait-indicator')",
:success => "Element.hide('wait-indicator')",
:complete => visual_effect(:appear, "recipe-status"),
:url => { :action => 'import', :id => @recipe.id },
:failure => "alert('Unable to import recipes at present')") %>
<-- Form goes here, I want this to be replaced if the submit succeeds -->
</div>
我能想到这样做的唯一方法是返回一个HTTP错误状态,如果有一个验证错误,但似乎像一个黑客攻击的一位。 有没有更干净的方法呢?
你是否建议我的函数返回JSON而不是模板?我确实认为这是一个解决方案,但它似乎很难将错误消息传回。 或者你只是建议我从Prototype切换到JQuery? – 2008-11-19 18:51:00
我建议换掉jQuery。无需JSON,观看上面链接的jQuery Railscasts,它基本上解释了您的任务所需的一切。 – mwilliams 2008-11-19 18:56:25