1

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

3
  • Why do you load files multiple times and in wrong order? Commented Nov 30, 2012 at 19:26
  • @Jani - you mean JS files ? I am not sure the order and yup will be removing the ones not required.. Commented Nov 30, 2012 at 19:27
  • Your jsFiddle is selecting Mootools instead of jQuery, but then you load jQuery as an external resource. Please read up on how to properly use jQuery and jQuery plugins. Commented Dec 1, 2012 at 7:16

2 Answers 2

2

Ehrm, I'm not much of an expert in ruby but seems like you are including both: jquery.validate and jquery.validate.min, and two different versions of jquery. That's not very good. But just out of curiosity, why don't you pop your jquery validate AFTER jquery?

Sign up to request clarification or add additional context in comments.

Comments

1

I agree with povilasp, you don't have to include both jquery.validate and jquery.validate.min file. The only difference between them is that the former one is debug version while the later one is the compress version.

Besides, if your rails version is quite new, you don't have to include the jquery explicitly as jquery is always the js library used by rails. (sorry, I don't remember the exact version)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.