I am trying to get some hands with Jquery client validation in rails ..
My following code works fine http://jsfiddle.net/hr1383/5CFRB/1/
"displaying error message correctly"
Now i am trying to do the same. Since i am not sure where to put the respective JS and CSS , so i decided to put in application.html.erb file to begin.
<html>
<head>
<title>Umvox</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag "jquery.validate" %>
<%= javascript_include_tag "jquery.validate.min" %>
<%= javascript_include_tag "jquery-1.7.2" %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"%>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"%>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tags %>
<script type="text/javascript">
$(document).ready(function() {
$("#company_form").validate({
rules: {
"company": {required:true}
},
messages:{
company: "Enter Company name"
}
});
});
</script>
<style type="text/css">
#company_form {background-color: aqua}
#company_form label { width: 250px; }
#company_form label.error, #company_form input.submit { margin-left: 253px; }
</style>
</head>
<body>
<%= render "shared/dashboard_button_login" %>
<%= yield %>
</body>
</html>
And my part of _form.html.erb contains
<%= form_tag("/locations/search" , :remote=> true, :id=>"company_form", :name=>"company_form", :form_to_validate=>'location') do %>
<table>
<tr>
<th>Company</th>
<th>City</th>
<th>Country</th>
<th>ZipCode</th>
</tr>
<tr>
<td><%= text_field_tag :company %></td>
<td><%= text_field_tag :city %></td>
<td><%= text_field_tag :country %></td>
<td><%= text_field_tag :zipcode %></td>
<td><%= submit_tag 'search'%></td>
</tr>
</table>
The following form converts into html as displayed in
http://jsfiddle.net/hr1383/5CFRB/1/
This doesnt seem to work and always making a server call without showing the error message.
Why is it happening ? Since the form doesnt belong to a certain model object so i dont know here to put the js ?
Update: I changed the application.html.erb to include only two files required :
<html>
<head>
<title>Umvox</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag "jquery.validate" %>
<%= javascript_include_tag "jquery-1.7.2" %>
<%= csrf_meta_tags %>
<script type="text/javascript">
$(document).ready(function() {
$("#company_form").validate({
rules: {
"company": {required:true}
},
messages:{
company: "Enter Company name"
}
});
});
</script>
<style type="text/css">
#company_form {background-color: aqua}
#company_form label { width: 250px; }
#company_form label.error, #company_form input.submit { margin-left: 253px; }
</style>
</head>
<body>
<%= render "shared/dashboard_button_login" %>
<%= yield %>
</body>
</html>
Thanks Harshit