1
  1. Hey, guys am stack with updating user password with react front end and devise, plus axios which am using to post that on an api.

  2. i have not changed anything from devise but when i hit the api the password is updated and i can use it for login but when i look at the terminal their is an error of ActionController::RoutingError (No route matches [PUT] "/")

  3. any help is highly appreciated

          const onFinish = (values) => {
             setLoading(true);
             const session = { "user": { ...values, reset_password_token: params.get("reset_password_token") } }
             const path = "/users/password"
             setAxiosHeaders();
             axios.put(path, session)
                 .then((response) => {
                     console.log(response.data)
                 })
                 .catch((error) => {
                     console.log(error);
                 })
         }
    
  1. when i hit the api it looks like it is being twice as it throws the same error.
  2. I have tried PATCH method also the same story.

1 Answers1

1

Javascript requests (xhr / fetch) redirect with the same http method as original request. You're sending a PUT request and your controller redirects to the root url /, request to the redirected url is issued with a PUT method.

Use status :see_other (aka 303) to redirect with a GET method:

redirect_to root_path, status: :see_other

You can set these options:

Devise.setup do |config|
  config.responder.error_status = :unprocessable_entity
  config.responder.redirect_status = :see_other
end

But it's probably best to set it up for a json response to begin with instead of html.

https://github.com/waiting-for-dev/devise-jwt/wiki/Configuring-devise-for-APIs

Alex
  • 16,409
  • 6
  • 40
  • 56
  • where can i put this redirect because i do not have devise controllers – mutebi godfrey Mar 17 '23 at 08:19
  • it throws out this error undefined method `responder' for Devise:Module (NoMethodError) – mutebi godfrey Mar 17 '23 at 22:21
  • i think the problem problem might be o how am handling the response or put request with axios. because the password is updated and it throws the error in the console – mutebi godfrey Mar 17 '23 at 22:23
  • @mutebigodfrey you have to update devise and responders to use these options. set axios `Accept` and `Content-Type` headers to `application/json`, if that doesn't change the response, try adding `respond_to :html, :json` to ApplicationController. https://github.com/heartcombo/devise#hotwireturbo – Alex Mar 18 '23 at 00:04
  • respond_to :html, :json i have already got this in my application controller but still its not getting right – mutebi godfrey Mar 18 '23 at 03:26