jameshwart lopez jameshwart lopez - 2 months ago 6x
Ruby Question

ajax Check if Email already exist Ruby on Rails

What im trying to do:

1.Send data to the server and check if that email exist in the database and return true if exist and false if it does not exist
What I have done so far:
I created this js code that fires on change keyup and paste.

$("#user_email").on('change keyup paste',function(){

//this request results to 404 not found.

I have this on rake routes

checkEmail POST /checkemail(.:format) user#emailcheck


Rails.application.routes.draw do
get 'products/index'

get 'home/index'

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'home#index'

resources :users, :products

# resources :users do
# resources :products
# end

# Example of regular route:
# get 'products/:id' => 'catalog#view'
post '/checkemail' => 'users#emailcheck', as: :checkEmail
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end

my users_controller.rb

def emailcheck
@user = User.search(params[:email])

user.rb mode

class User < ActiveRecord::Base
validates :terms_of_service, acceptance:{ :accept => '0'}
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i

validates_presence_of :email,:password,:firstname,:lastname,:address,:phonenumber
has_many :products

def self.search(email)
if email
where('email = ?',email).first


I dont know how to point how to use the method inside my users_controller
using ajax ang create a response of true or false base on the results.can some one help me do this because im relatively new to ruby on rails

Error Log

POST http://localhost:3000/checkemail?email=sample 404 (Not Found)jQuery.ajaxTransport.send @ jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1:9660jQuery.extend.ajax @ jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1:9211jQuery.each.jQuery.(anonymous function) @ jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1:9357(anonymous function) @ user.self-4e8aa3f9430109eadc33d5aa4fd038041afbf947d7c95717d55caf908ce3f0df.js?body=1:5jQuery.event.dispatch @ jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1:4666jQuery.event.add.elemData.handle @ jquery.self-d03a5518f45df77341bdbe6201ba3bfa547ebba8ed64f0ea56bfa5f96ea7c074.js?body=1:4334

rails console

Started POST "/checkemail?email=d" for at 2015-07-23 15:18:21 +0800

ActionController::RoutingError (uninitialized constant UserController):
activesupport (4.2.3) lib/active_support/inflector/methods.rb:261:in `const_get'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:261:in `block in constantize'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:259:in `each'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:259:in `inject'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:259:in `constantize'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:72:in `controller_reference'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:62:in `controller'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:41:in `serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:821:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.3) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.3) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.3) lib/active_support/callbacks.rb:84:in `run_callbacks'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.3) lib/rails/engine.rb:518:in `call'
railties (4.2.3) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/admin-new/.rbenv/versions/2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/home/admin-new/.rbenv/versions/2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/home/admin-new/.rbenv/versions/2.2.2/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'


It is a typo, just make it like this and try:

$("#user_email").on('change keyup paste',function(){


And in the routes make it to this:

post '/checkemail', to: 'users#emailcheck'

Or whatever you have written make this: user#emailcheck to users#emailcheck as controllers are always in plural form.

In your users_controller make it:

def emailcheck
  @user = User.search(params[:email])
  respond_to do |format|
    format.json {render :json => {email_exists: @user.present?}} #sir Deep suggestion to return true or false for email_exists or the code below
   # format.json {render :json => @user} #this will output null if email is not in the database

As you need json response.

Hope this helps.