简体   繁体   中英

Rails 6.0.0 TypeError - no implicit conversion of nil into String:

people so I have this problem when trying to do a post API call. the situation is this: I have a large application where students sign up so I can manage them and contact them through that student management app. Now I have created a smaller app where students can do their assignments, lets call that homework app; I am doing a post API call to the bigger app in the login section so students don't have to sign up again and they can use the same information in both applications. I am getting an internal server error 500; in the terminal the error is:

TypeError - no implicit conversion of nil into String:
 app/controllers/api/sessions_controller.rb:12:in `create'

this is the create action in sessions controller in the student management app:

  def create
    unless request.format == :json
      sign_out
      render status: 406, json: { message: "JSON requests only." } and return
    end
    resource = warden.authenticate!(auth_options)

    if resource.blank?
      render status: 401, json: { response: "Access denied." } and return
    end

    sign_in(resource_name, resource)
    respond_with resource, location:
    after_sign_in_path_for(resource) do |format|
      format.json { render json:
        {
          success: true, jwt: current_token, response: "Authentication successful"
        }
      }
    end

  end

and most importantly the api call with a very simple html form: ps: I am running the projects locally, thats why the url's are defined as localhost

<form class="login-form">
  <input type="email" class="mail mail-input" placeholder=" Email"><br>
  <input type="password" class="password pw-input" placeholder=" Password"><br>
  <button class="login" type="button" name="button">Login</button>
</form>

<script>

var login = document.querySelector(".login")
login.addEventListener("click", function(){
  var email = document.querySelector(".mail")
  var password = document.querySelector(".password")
  let loginValues = {
    email: email.value,
    password: password.value
  }
  $.post (
   "http://localhost:4000/api/login.json",
    {
      api_user: {
        email: email.value,
        password: password.value
      }
    },
    function (response){
      console.log(response)
      console.log('in');
      window.location.replace("http://localhost:3000/")
    }
  )
})

</script>

Can you try replacing this:

api_user: {
  email: email.value,
  password: password.value
}

With this:

user: {
  email: email.value,
  password: password.value
}

Tell me if it works?

Update, based on comments:

I really have no idea what you are doing in the code above, the code is really messy... Try replacing this:

respond_with resource, location:
after_sign_in_path_for(resource) do |format|
  format.json { render json:
    {
      success: true, jwt: current_token, response: "Authentication successful"
    }
  }
end

with this:

respond_with resource do |format|
  format.json { render json:
    {
      success: true, jwt: current_token, response: "Authentication successful"
    }
  }
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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