scientiffic
scientiffic

Reputation: 9415

Routing Error: No Route Matches

I'm getting a routing error even though I believe the action exists in my controller.

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:

 $("#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?

Upvotes: 0

Views: 108

Answers (4)

Matthias
Matthias

Reputation: 4375

This is not a direct answer to your question, but maybe helpful.

Do you know the client_side_validations gem? I think you can use it, instead of implement everything by your own. It is available on github.

Upvotes: 0

craig.kaminsky
craig.kaminsky

Reputation: 5598

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. The actual URL for your route would be /users/validate_email instead of /users/:id/validate_email

The :email part of the route would make params[:email] available in your controller method.

Upvotes: 2

Zippie
Zippie

Reputation: 6088

You somehow have to pass in the :id

validate_email_user_path user.id

or

validate_email_user_path user

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160271

You need to provide the URL helper with a user.

Upvotes: 0

Related Questions