Hi I'm trying to run Railscast's #241 - using Omniauth for twitter - eventually with facebook.The app I'm trying to make will require Facebook's login, but since there is a video on how to use twitter to login, might as well follow it first but I'm bumping into problems.
I keep getting this error after I signed into Twitter. "NoMethodError in SessionController#create" - undefined method `[]' for nil:NilClass. Here is the related code.
class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
session[:user_id] = user.id
redirect_to root_url, :notice => "Signed in!"
end
end
I have also set the website and callback link via dev.twitter.com as follows: website: "http://127.0.0.1:3000" Callback: "http://127.0.0.1:3000/auth/twitter/callback"
Omniauth.rb in config/initializers:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'
end
(I've already provided the consumer key and secret)
Application.html.erb
<div id="user_nav">
<%= link_to "Sign in with Twitter", "/auth/twitter" %>
</div>
Added this line in routes.rb
match "/auth/:provider/callback" => "sessions#create"
User.rb
class User < ActiveRecord::Base
attr_accessible :name, :provider, :uid
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["user_info"]["name"]
end
end
I don't know where I went wrong