John
John

Reputation: 481

Validation jQuery, remote is redirecting wrong in Rails 3

In application.js ...

$(document).ready(function () {
$("#new_academic_formation").validate({
debug: true,
rules: {
      "academic_formation[id]": {
      required: true,  
      minlength: 3, 
      remote:  "/academic_formations/check_id"
      }
}     
});
});

In my academic_formations_controller.rb

def check_id
    @academic_formation = AcademicFormation.find(params[:id])
    respond_to do |format|
      format.json { render :json => !@academic_formation }
    end
end

And finally in file routes.rb

resources :academic_formations
match "/academic_formations/check_id" => 'academic_formations#check_id'

The problem is that instead of calling the action check_id rails is calling the action show.

Upvotes: 2

Views: 215

Answers (1)

prasvin
prasvin

Reputation: 3009

You'll need to move match "/academic_formations/check_id" => 'academic_formations#check_id' before resources :academic_formations.

This is straight from the docs.

"Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action’s route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first."

OR

Since yours is a resourceful route, you may add a route as, if it applies to a collection(you can read more about that in the docs linked above) and if it's a get method as :

resources :academic_formations do
  collection do
    get 'check_id'
  end
end

Upvotes: 1

Related Questions