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

佐手、 提交于 2019-12-02 11:51:27

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!