Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client Side Validations doesn't work in production, but works in development

This is my User model:

class User < ActiveRecord::Base
    rolify
  devise :database_authenticatable, :registerable, :token_authenticatable, :confirmable, :timeoutable, 
         :recoverable, :rememberable, :trackable, :validatable,
                 :email_regexp =>  /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :confirmed_at, :confirmation_token
    validates_uniqueness_of :email, :case_sensitive => false
    validates_confirmation_of   :password

end

Gemfile:

gem 'client_side_validations'

Form:

<%= form_for(resource, :as => resource_name, :class => "send-with-ajax", :url => user_registration_path(resource), :validate => true, :html => { :id => 'user_new' }) do |f| %>
              <%= f.text_field :email,:placeholder => "[email protected]", :input_html => {:autofocus => true}, :validate => true %>
              <%= f.submit :label => "Submit", :value => "Sign Me Up!"  %>
      <div class="ajax-response"><%= devise_error_messages! %></div>
          <% end %>

It should work when you tab out of the field, here is a screenshot of it working locally:

client_side_validation local

But when I do it in production, it doesn't work at all and I don't see any errors in the log file or in the JS console.

Edit 1:

Validation messages are not showing up.

In Heroku logs, I see this whenever I try a 'valid' email address:

2012-06-10T18:44:55+00:00 app[web.1]: Started GET "/validators/uniqueness?case_sensitive=false&user%5Bemail%5D=abc%40tell.com" for xx.xxx.xx.x2 at 2012-06-10 18:44:55 +0000
2012-06-10T18:44:55+00:00 heroku[router]: GET myapp.heroku.com/validators/uniqueness?case_sensitive=false&user%5Bemail%5D=abc%40tell.com dyno=web.1 queue=0 wait=0ms service=221ms status=404 bytes=4

But it doesn't show me a message on my page like it does locally.

Also, the only time I see that HTTP request in the log is when I try a string that has a 'valid' email structure - like [email protected]. But if I try something like abc it doesn't give me the message I expect - invalid address - nor does the log show anything.

Edit 2:

I tried running my local server in production mode and I am seeing the same issue as on Heroku. So it seems the issue may not have anything to do with Heroku - but rather production.

This is the HTML generated from the form in production:

<form accept-charset="UTF-8" action="/users" class="formtastic user" data-validate="true" id="user_new" method="post" novalidate="novalidate"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="kQid^%&^%jhgdsjhgfxmw4=" />
              <input data-validate="true" id="user_email" input_html="{:autofocus=&gt;true}" name="user[email]" placeholder="[email protected]" size="30" type="text" />
              <input label="Submit" name="commit" type="submit" value="Sign Me Up!" />
</form><script>window['user_new'] = {"type":"Formtastic::FormBuilder","inline_error_class":"inline-errors","validators":{"user[email]":{"presence":{"message":"can't be blank"},"uniqueness":{"message":"has already been taken","case_sensitive":false},"format":{"message":"is invalid","with":/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,"allow_blank":true}}}};</script>               </div>

Edit 3:

This is how my application.js looks:

//= require jquery
//= require jquery_ujs
//= require rails.validations
//= require_tree .

And in my application.html.erb layout file I just have a JS call to application.js

Edit 4:

I thought it may have been a situation where the JS script generated by client_side_validation is not recognizing the input because the id is user_email and not user[email]. So I changed the id for the input to user[email] and that did not solve it.

Anyone have any thoughts?

like image 601
marcamillion Avatar asked Nov 04 '22 22:11

marcamillion


1 Answers

Best guess is that this tracks to the asset pipeline. Some other gems may be preventing assets from correctly compiling.

I don't have a perfect answer for you, but the Heroku docs may be of some help here: https://devcenter.heroku.com/articles/rails3x-asset-pipeline-cedar

like image 50
Calciphus Avatar answered Nov 09 '22 05:11

Calciphus