5

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 => "your-email@address.com", :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 abc@email.com. 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="your-email@address.com" 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?

marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • Can you load the webpage using the Firebug or Web Inspector and post here the error if one? It may be the js code that's not running properly. –  Jun 14 '12 at 14:42
  • Also try reading the response of the AJAX call (enable logging of Request/Responses in your preferred debugging tool). It might also be the controller response changing in production, but it's more likely to be a js issue, not a backend one. –  Jun 14 '12 at 14:47
  • @lexor I couldn't find anything out of the ordinary, but I don't use Firebug too much, so I may be missing something. Here is a production version - http://falling-sunset-7904.herokuapp.com/ If you type in `abc` and tab out, in production you should see 'is invalid'. In Firebug, I do see that jQuery info being loaded...soo...not sure what's happening. – marcamillion Jun 14 '12 at 21:23
  • Change `:input_html => {:autofocus => true}` with `:autofocus => true`, this should solve the output error where you get an attribute `input_html="{:autofocus=>true}"`, btw I'd also rely on jquery for this stuff. Regarding your issue I couldn't find a function that gets executed when the email is not in the form "name@domain.com" (so using "name@domain" does nothing). When using a valid email it's calling the uniqueness URL receiving a 404. Is the client_side_validations gem available inside your production environment? Try listing out your gems and see what version are you using please. –  Jun 15 '12 at 08:58
  • This is the only js file available (excluding ga.js --google analytics) http://falling-sunset-7904.herokuapp.com/assets/application-b5f7c629f9622da89d5cc703e459e6c3.js You can check it out, it appears to be only jQuery core. Maybe try with something like `<%= javascript_include_tag 'rails.validations' %>` just to see if it includes rails validations. –  Jun 15 '12 at 09:04
  • Does asset precompilation affecting something in js? – chaitanya Jun 18 '12 at 12:49
  • @lexor These are the gems & versions - https://gist.github.com/461f12efd140a16894d6 – marcamillion Jun 18 '12 at 20:46
  • @lexor `client_side_validations` gem is not in a `production` group. It applies site-wide, in all environments. Also, when I run it in development, I see a `js` script that is inserted right near the form that handles the validation: `` – marcamillion Jun 18 '12 at 20:51
  • @lexor You may be on to something, because in the local version, I am seeing a bunch of other `.js` files that I am not seeing on the production version. Do I have to manually specify the names of all the files in the `javascript_include_tag`? I thought that once I had `//= require_tree .` in the application.js, and I did `<%= javascript_include_tag "application" %>` it would include all the JS files? Is that not so? – marcamillion Jun 18 '12 at 21:00
  • @lexor it seems that the asset pipeline compiled it all into that one Application.js link. For instance, this is the `rails.validations.js` file in my development - https://gist.github.com/515a7eb5aa74a476c490 - if you go through that and search for key pieces of code, you will find it in the application.js file (albeit, quite minimized and compressed). You can also search that `application.js` file for `clientSideValidations.callbacks` and you will see multiple instances. So everything is compiled into 1. – marcamillion Jun 18 '12 at 21:27
  • @chs The only thing I have read that it does is compresses all the JS files into one `application.js` file. – marcamillion Jun 18 '12 at 21:40
  • Please do a [paste](http://pastie.org/) of the local working HTML, or even better: spin up another Heroku instance and run your app in development mode on it, then send us the link. – opyate Jun 19 '12 at 09:42

1 Answers1

2

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

Calciphus
  • 146
  • 1
  • 13