简体   繁体   English

路由错误:没有路由匹配

[英]Routing Error: No Route Matches

I'm getting a routing error even though I believe the action exists in my controller. 即使我认为该操作存在于控制器中,也遇到路由错误。

routes.rb: routes.rb中:

Build::Application.routes.draw do
  resources :users do
     match 'users/:id' => 'users#username'
     get 'validate_username', on: :member
     get 'validate_email', on: :member
  end
end

new.html.erb: new.html.erb:

 $("#user_email").on('blur', function(){
    if($(this).val()!=""){
        user_email = $(this).val();
        $.ajax({
            url:"<%=validate_email_user_path%>",
            type:'GET',
            data: {email: user_email},
            success: function(data,status,xhr){
                console.log(data);
                alert(data.message);
        });

rake routes: 耙路:

  validate_email_user GET    /users/:id/validate_email(.:format)                         users#validate_email

What am I doing wrong? 我究竟做错了什么?

You somehow have to pass in the :id 您必须以某种方式传递:id

validate_email_user_path user.id

or 要么

validate_email_user_path user

If you want to pass in just the email and not an existing user, then I would think you need to update your routes. 如果您只想传递电子邮件而不是现有用户,那么我认为您需要更新您的路线。

This: 这个:

get 'validate_email', on: :member

Should be 应该

get 'validate_email/:email', on: :collection

Within the users 'collection', you do not need to pass a user/user id into the _path helper. 在用户的“集合”中,您无需将用户/用户ID传递到_path帮助器中。 The actual URL for your route would be /users/validate_email instead of /users/:id/validate_email 您路线的实际URL为/ users / validate_email,而不是/ users /:id / validate_email

The :email part of the route would make params[:email] available in your controller method. 路由的:email部分将使params [:email]在您的控制器方法中可用。

您需要向用户提供URL帮助器。

This is not a direct answer to your question, but maybe helpful. 这不是您问题的直接答案,但可能会有所帮助。

Do you know the client_side_validations gem? 您知道client_side_validations宝石吗? I think you can use it, instead of implement everything by your own. 我认为您可以使用它,而不用自己实现所有功能。 It is available on github. 它可以在github上找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM