Rails: External API Integration using RestClient (undefined local variable or method `user')

后端 未结 1 332
说谎
说谎 2021-01-28 01:57

I am building a digital library, and I have completed a lot of the functionalities needed. I am currently having an issue with integrating the digital library with a Learning Ma

相关标签:
1条回答
  • 2021-01-28 02:49

    In your sessions controller, the line session[:user_id] = user.id, user is undefined i.e. You never assigned a value to the user variable.

    Assuming your User database(in digital library, not LMS) have the record of user currently logging in, then you should use something like this:

    when 200
      session[:user_id] = response.body.data.user.id
      redirect_to root_url, notice: 'Logged in!'
    

    Now another case, When the user signs up in your LMS, then when he visits digital library app, and tries to login, he won't be able to. Because your digital library app doesn't have the user linked with LMS or knows anything about LMS. So, whenever user creates a session, you'll need to check if User record is present in your digital library DB or not, if not create one. You can do something like this:

    when 200
      if User.find(response.body.data.user.id).present?
        session[:user_id] = response.body.data.user.id
        redirect_to root_url, notice: 'Logged in!'
      else
        user = User.create(id: response.body.data.user.id, username: response.body.data.user.username, passord: params[:password], password_confirmation: params[:password])
        session[:user_id] = response.body.data.user.id
        redirect_to root_url, notice: 'Logged in!'
      end
    

    The above method is just for reference, you must add more validations if required.

    0 讨论(0)
提交回复
热议问题