所以我试图在Rails 3.1中建立一个评论系统。我们的想法是让client_side_validations gem验证基本表单,并在成功时使用Ajax动态添加它。在我添加gem之前,一切正常(错误处理除外)。添加gem后,页面将重新加载表单提交。 client_side_validations gem似乎出错了,这可能导致了这一点 - 我不知道如何/为什么。我得到的Javascript错误是:
TypeError: 'undefined' is not an object (evaluating 'validators[this[0].name]')
我的表单代码:
= simple_form_for([@post, @post.comments.build], :remote => true, :validate => true) do |f|
#comment-left
= f.input :name
= f.input :email
= f.input :website
#comment-right
= f.input :content, :as => :text
= f.button :submit
控制器代码:
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(params[:comment])
respond_to do |format|
if @comment.save
format.html { redirect_to post_path(@post) }
format.js
else
format.html { redirect_to post_path(@post) }
format.js
end
end
end
Create.js.erb代码:
$('<%= escape_javascript render(:partial => @comment) %>').hide().insertBefore('#new_comment').slideDown();
最后,部分代码:
.comment
= image_tag(comment.gravatar_url)
%span
Posted by
- unless comment.website.blank?
= link_to comment.name, comment.website
- else
= comment.name
on
= comment.created_at.strftime('%m.%d.%Y')
%p= comment.content
我认为这一切都是相关的 - 如果您需要型号代码,只需这样说,我就会提供。但是没有使用自定义验证器。任何有关为什么client_side_validations不起作用的见解将非常感激。谢谢!
答案 0 :(得分:0)
请参考此问题并尝试一下,
client_side_validations (3.1.0) not working when new form is added to the DOM
如果没有解决, 告诉我。