Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ajax检查Ruby on Rails上是否已经存在电子邮件_Ruby On Rails_Ruby_Ajax - Fatal编程技术网

Ruby on rails ajax检查Ruby on Rails上是否已经存在电子邮件

Ruby on rails ajax检查Ruby on Rails上是否已经存在电子邮件,ruby-on-rails,ruby,ajax,Ruby On Rails,Ruby,Ajax,我想做的是: 1.向服务器发送数据,检查数据库中是否存在该电子邮件,如果存在则返回true,如果不存在则返回false 到目前为止我所做的: 我创建了这个js代码,它在更改keyup和粘贴时触发 $("#user_email").on('change keyup paste',function(){ console.log("change?"); $.post('/checkEmail?email='+$("#user_email").val(),function(dat

我想做的是:
1.向服务器发送数据,检查数据库中是否存在该电子邮件,如果存在则返回true,如果不存在则返回false 到目前为止我所做的: 我创建了这个js代码,它在更改keyup和粘贴时触发

$("#user_email").on('change keyup paste',function(){
        console.log("change?");
    $.post('/checkEmail?email='+$("#user_email").val(),function(data){

    }); 
   //this request results to 404 not found.
});
我在rake路线上有这个

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

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
end
我的用户\u controller.rb

 def emailcheck
        @user = User.search(params[:email])
 end
user.rb模式

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
        end

    end
end
轨道控制台

Started POST "/checkemail?email=d" for 127.0.0.1 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'

这是一个输入错误,只需这样做,然后尝试:

$("#user_email").on('change keyup paste',function(){
        console.log("change?");
    $.post('/checkemail?email='+$("#user_email").val(),function(data){

    });
});
在路线上做到这一点:

post '/checkemail', to: 'users#emailcheck'
或者,无论你写了什么,都要这样做:
user#emailcheck
users#emailcheck
,因为控制器总是复数形式

在您的
用户\u控制器中
执行以下操作:

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
  end
end
您需要
json
响应


希望这有帮助。

您可以检查控制器中的
参数[:email]。是否显示?
。如果params[:email]不存在,则无需调用
搜索
方法。谢谢,请发布一个404响应的日志文件。先生,我用特定的错误登录更新了日志,意思是rails日志文件。@jameshwartlopez向我们显示一个完整的
config/routes.rb
文件。如果有任何请求,请在rails控制台上发布完整的登录。有错误日志吗?请将结果发布到内部服务器错误模板缺少sirLet us。您可以省略
respond_to
块,只需
render:json=>msg
即可。
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
  end
end