Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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
Jquery Rails 3.2应用程序中Ajax调用出现404/500错误(已检查路由)_Jquery_Ruby On Rails_Ajax_Validation_Http Status Code 404 - Fatal编程技术网

Jquery Rails 3.2应用程序中Ajax调用出现404/500错误(已检查路由)

Jquery Rails 3.2应用程序中Ajax调用出现404/500错误(已检查路由),jquery,ruby-on-rails,ajax,validation,http-status-code-404,Jquery,Ruby On Rails,Ajax,Validation,Http Status Code 404,我正在尝试编写一个Ajax调用,它将检查用户在表单中输入的电子邮件地址是否已经存在于数据库中。我已经检查了又检查了我的路线,在这里找不出问题所在。有多个类似的SO问题,但在大多数问题中,问题似乎是有人将路由定义为get in routes.rb,但使用post进行Ajax调用,反之亦然。我得到的错误是:POSThttp://localhost:3000/registrations/validate_uniqueness 404找不到 更新 我将控制器操作更改为如下所示: def vali

我正在尝试编写一个Ajax调用,它将检查用户在表单中输入的电子邮件地址是否已经存在于数据库中。我已经检查了又检查了我的路线,在这里找不出问题所在。有多个类似的SO问题,但在大多数问题中,问题似乎是有人将路由定义为get in routes.rb,但使用post进行Ajax调用,反之亦然。我得到的错误是:POSThttp://localhost:3000/registrations/validate_uniqueness 404找不到

更新 我将控制器操作更改为如下所示:

  def validate_uniqueness
    respond_to do |format|
      format.json do
        if User.find_by_email(params[:email])
          render :json => { value: true }
        else
          render :json => { value: false }
        end
      end
    end
  end
仍然得到404错误

第二次编辑 了解了如何使用Chrome的开发工具查看有关我的Ajax请求的更多信息。这是完整的错误消息,来自Desive:

Unknown action

Could not find devise mapping for path "/registrations/validate_uniqueness".This may happen for two reasons:1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: @request.env["devise.mapping"] = Devise.mappings[:user]
因此,我将routes.rb改为:

这是朝着正确方向迈出的一步,但现在我得到了:


未捕获引用错误:未定义值

如果您只想要成功/失败响应,请在控制器中使用此选项:

def validate_uniqueness
  if User.find_by_email(params[:email])
    head :no_content   # returns 204 NO CONTENT and triggers success callback
  else 
    head :not_found    # returns 404 NOT FOUND and triggers error callback
  end
end
然后删除对JS中响应值的任何引用,因为除了HTTP状态码204之外,什么都没有


使用404指示查找不成功是明智的响应。你应该用它

您忘记了validate\u唯一性方法上的“end”子句。干杯是的,刚刚注意到了这一点,并添加了第四个端点。仍然得到错误!能否打印本地web服务器日志记录输出?你是如何启动你的应用程序的?刚刚把输出添加到post的末尾是的,做了更改。现在我在处理响应或其他方面遇到了问题,因为它告诉我,在呈现json行期间,我的控制器操作中的值是未定义的,并且响应和结果也是未定义的
  def validate_uniqueness
    respond_to do |format|
      format.json do
        if User.find_by_email(params[:email])
          render :json => { value: true }
        else
          render :json => { value: false }
        end
      end
    end
  end
Unknown action

Could not find devise mapping for path "/registrations/validate_uniqueness".This may happen for two reasons:1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: @request.env["devise.mapping"] = Devise.mappings[:user]
  devise_scope :user do 
    post '/registrations/validate_uniqueness' => 'registrations#validate_uniqueness'
  end
def validate_uniqueness
  if User.find_by_email(params[:email])
    head :no_content   # returns 204 NO CONTENT and triggers success callback
  else 
    head :not_found    # returns 404 NOT FOUND and triggers error callback
  end
end