Rails for Beginners Part 25: Twitter Account Model Discussion
Hi! After setting the twitter_account model, before accessing Ruby console and running the "User.last.twitter_accounts" command, shouldn't we run 'rails db:migrate' to actually create the table in the database? I'm asking cause I was only able to get the query to complete after running the migrate command.
Hello friends! I connected to my twitter account using the connect button but I forgot to connect in the sign_in option.
So I received this kind of error " undefined method `twitter_accounts' for nil:NilClass" because I did not make this error bypass. How should I do to send a message to remmeber the user to connect via sign_in first? It is better to use just an alert or create a controller and all the rest if I got NilClass.
Why i don't have records of twitter accounts in the database? You can see it in the image
I don know why, but i managed to get the info creating a new account on the project. The old ones never get the twitter account
If you want to get it fixed, you may modify omniauth_callbacks_controller.rb and check if Current.user has any twitter accounts associated. If not, you have to create an entry in TwitterAccount table with Current.user[:id]. Looks like this:
class OmniauthCallbacksController < ApplicationController before_action :require_user_logged_in! def twitter if Current.user.twitter_accounts.any? twitter_account = Current.user.twitter_accounts.where(username: auth.info.nickname).first_or_initialize twitter_account.update( name:auth.info.name, username: auth.info.nickname, image: auth.info.image, token: auth.credentials.token, secret: auth.credentials.secret, ) redirect_to twitter_accounts_path, notice: "Conta do twitter conectada com sucesso!" else # If there is no twitter account associated TwitterAccount.create(user_id: Current.user[:id],username: auth.info.nickname, name:auth.info.name, image: auth.info.image, token: auth.credentials.token, secret: auth.credentials.secret, ) redirect_to twitter_accounts_path, notice: "Conta do twitter conectada com sucesso!" end end def auth request.env["omniauth.auth"] end end
Hi, I'm getting the following error "undefined method `twitter_accounts' for nil:NilClass". i can view the twitter_account model and in the user model i have also included the following "has_many :twitter_accounts". from the rails cons the hash i can also view the details to be insterted into the db. please assist.
I've followed the updated instructions and I've run into some intermittent behavior where I get the
when (400..499) raise OAuth::Unauthorized, response
response myself. The best I can determine, if I start from the Connect Twitter button and get a Rails error partway through (say because I didn't call
redirect_to root_path in
OmniauthCallbacksController::twitter), then refreshing the page will lead to the above Unauthorized error.
So if you're getting this error, try fixing any underlying Rails errors and then reload the home page and click the button and see if it works!