All threads / Rails for Beginners Part 25: Twitter Account Model Discussion

Ask A Question

Notifications

You’re not receiving notifications from this thread.

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.

yeah actually he does the db:migrate after the creation of the model minute 1:20

Reply

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.

Cris answered in episode 27 just go there and fix the error in the class omniauth. Great work Cris.

Reply

Why i don't have records of twitter accounts in the database? You can see it in the image
https://ibb.co/74T6GNn

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

Thank you so much Murilo! your solution solved it!

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

Reply

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.

How did you manage to resolve this issue?

Well, it seems I need to log in first.

Reply

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!

Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 54,645+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.