0

I have worked with jQuery remote function in the past, but in this case I'm having trouble.

I have a working remote function to check email uniqueness, but now I want to check user password, when he enters it using the jQuery validation plugin (if it is possible) but I constantly get in Firebug :

 406 Not acceptable

Here is my action(PROBLEM IN HERE. EDIT): -EDIT

def checkpass 
 @user = User.find_by_email(params[:email])
  if  @user && @user.valid_password?(params[:user][:password])
   @check = true
  end
  respond_to do |format|
   format.json { render :json => @check }
 end
end

From console: -EDIT

    Started GET "/checkpass?user%5Bpassword%5D=[FILTERED]&email=new%40mail.com" for   127.0.0.1 at 2012-09-30 11:42:26 +0300
   Processing by EditUserController#checkpass as JSON
   Parameters: {"user"=>{"password"=>"[FILTERED]"}, "email"=>"new@mail.com", "pas
   sword"=>"[FILTERED]"}
 ←[1m←[36mUser Load (0.0ms)←[0m  ←[1mSELECT "users".* FROM "users" WHERE "users
   "."email" = 'new@mail.com' LIMIT 1←[0m
  User: new@mail.com
  Pass: 123456
  Devise pass:true
  Completed 200 OK in 438ms (Views: 15.6ms | ActiveRecord: 0.0ms)

Here is WORKING action for checking email uniqueness :

 def checkemail
  @user = User.find_by_email(params[:user][:email])
   respond_to do |format|
    format.json { render :json => !@user }
   end
end

and my jQuery validation with remote function -EDIT

     $("#edit_password").validate({
   rules: {

        "user[password]":{
            required: true,
            minlength: 6,
            remote: {
            url: "http://127.0.0.1:3000/checkpass",
            type: "get",
            data: {
              email: function() { return $("#user_email").val(); }
            }
        },
   ...
  })

Working example of checkuniqeness jQuery:

    "user[email]": {
          required: true,
          email: true,
          remote: {
          url: "http://127.0.0.1:3000/checkemail"
          }

Where is my error ?

MID
  • 1,815
  • 4
  • 29
  • 40
  • Try changing the url to "http://127.0.0.1:3000/checkpass.json". – Frost Sep 26 '12 at 12:38
  • @Frost, tried. Not working. In other requests I didn't use json extension. I will give you working example. – MID Sep 26 '12 at 12:41
  • In your `checkpass` is this a typo that you `render :json => @user`? Because earlier you are assigning `user` local variable, and `user` is checked in conditional. – Casual Coder Sep 26 '12 at 14:59
  • @Casual, it is not error. It is typo in question. – MID Sep 27 '12 at 06:00
  • Ok, please edit the code to be without typos and omissions (`respond_to` in `checkpass` has no ending `end`). The other thing is that when the authentication is failing there is no `format.json` block. In `checkmail` you are always returning json, even if it is a nil. In the `checkpass` it is not the case. – Casual Coder Sep 27 '12 at 16:03
  • @Casual, if it will be nil - validation will fail and tell me that. My code for email checking is working. – MID Sep 27 '12 at 21:23
  • Exactly what I was talking about. Your code in `checkmail` is working because regardless of `@user` you are returning `format.json` from the controller, and your client-side code gets the json response (even if it is a nil). In the `checkpass` it is different. When the `user && user.authenticate(params[:user][:password])` condition fails what is returned to the client ? – Casual Coder Sep 28 '12 at 04:45
  • @Casual, please check action with logger debug. – MID Sep 28 '12 at 06:45
  • Now it fails on `authenticate` (NoMethodError). Are you sure, you can check password this way? If you're using devise, the way to do it, is with `user.valid_password?(params[:user][:password])`. Debug it one step at a time. Fire up `rails console` get the user by mail, and check this `authenticate` bit to be sure that it is working. – Casual Coder Sep 28 '12 at 07:58
  • @Casual, please look at changed action. Error stays the same. – MID Sep 28 '12 at 10:05
  • I've looked at it. I've tried to simulate it in my project. And it worked. I've set up a controller with such method like `checkpass` give similar `format.json` response, and query it from jquery ajax call, and it worked. So I simply do not know what the problem is in your case. – Casual Coder Sep 29 '12 at 09:43
  • @Casual, action is performing OK, but I can't login. Can you look at my code again ? – MID Sep 30 '12 at 08:41
  • Did you solve it? In code in the question there are only ajax actions for validation. I suppose user credentials are edited in the form but are they saved in DB? Perhaps in update action? – Casual Coder Oct 03 '12 at 06:27
  • @Casual, I solve it with not very good solution) I will show you) – MID Oct 03 '12 at 06:58
  • 1
    Ok, but if you want to return only true/false in json the better option will be `format.json { render :json => (@user && @user.valid_password?(params[:user][:password])) }` and you get rid of conditional with this @check flag. – Casual Coder Oct 03 '12 at 15:53

0 Answers0