2013-05-16 31 views
0

我在探索ROR并坚持简单的事情,即验证。 我在application.html.erb中添加了jquery.min.js和jquery.validate.min.js。然后,我在模型文件(music_file.rb)中添加了Rails jquery验证不起作用

validates :title, :presence => true 

查看代码

<%= form_for(@music_file, :url=>{:action => @post_url}, :multipart => true,  
:method=>"post", :id=>"upload_song_form") do |f|%> 
<%= f.text_field :title, :class => "input-textarea required"%> 

在提交按钮确认不点火。我没有在这里使用宁静的网址,因为根据之前的网页逻辑,发布的网址是动态的。这是这个问题的根源吗?

+0

不确定,但我不认为jquery验证挂钩到AR模型。您必须为此编写额外的客户端代码。另外:走出宁静总是会让你头疼。也许Rails是这个项目的错误选择。 – Wukerplank

回答

0

我经常这样做:

_form.html.erb

<%= semantic_form_for @article, remote: true do |f| %> 
    <%= f.semantic_errors %> 

    <%= f.inputs do %> 
    <%= f.input :title %> 
    <%= f.input :content %> 
    <% end %> 

    <%= f.actions do %> 
    <%= f.action :submit %> 
    <% end %> 

<% end %> 

这将启动创建行动,并与远程真正的选项,它会火起来create.js.erb文件,所以你可以检查任何错误(验证),并填充如果需要:

<% if @article.errors.blank? %> 
    // some code goes here 
<% else %> 
    $("#info_window").html("<%=j render "form" %>"); 
<% end %>