ActionController::UnknownFormat error (missing template) in Users::OmniauthCallbacksController#facebook when using Devise gem in a rails application? -


i'm attempting finish facebook omniauth integration , ran error below. i'm not sure what's causing happen. occures second time try log in using facebook auth. first time (on user create) works perfectly. if log out , hten log in throws error.

here's error

actioncontroller::unknownformat in users::omniauthcallbackscontroller#facebook  users::omniauthcallbackscontroller#facebook missing template request format , variant. request.formats: ["text/html"] request.variant: [] note! xhr/ajax or api requests, action respond 204 no content: empty white screen. since you're loading in web browser, assume expected render template, not nothing, we're showing error extra-clear. if expect 204 no content, carry on. that's you'll xhr or api request. give shot. 

user.rb

class user < applicationrecord   # include default devise modules. others available are:   # :confirmable, :lockable, :timeoutable , :omniauthable    devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable, :omniauthable   has_many :services   has_paper_trail   scope :referred, -> { where.not(:referral_id => nil) }  end 

omniauth_callbacks_controller.rb

class users::omniauthcallbackscontroller < devise::omniauthcallbackscontroller   before_action :send_referral_event_email, only: [:create]     def facebook     service = service.where(provider: auth.provider, uid: auth.uid).first      if service.present?       user = service.user       service.update(         expires_at: time.at(auth.credentials.expires_at),         access_token: auth.credentials.token,       )     else       user = user.create(          email: auth.info.email,          name: auth.info.name,          password: devise.friendly_token[0,20]       )       user.services.create(          provider: auth.provider,          uid: auth.uid,          expires_at: time.at(auth.credentials.expires_at),          access_token: auth.credentials.token,       )       if session[:cte_id]         usermailer.delay.send_referred_message(user)         sign_in_and_redirect user, event: :authentication         set_flash_message :notice, :success, kind: "facebook"       else         actionwins.create_advocate(user.email)         usermailer.delay.advocate_registration_email(user)         sign_in_and_redirect user, event: :authentication         set_flash_message :notice, :success, kind: "facebook"       end     end    end    def auth     p request.env['omniauth.auth']   end    def after_sign_in_path_for(resource)     if session[:cte_id]       static_thankyou_path     else       root_url     end   end    def send_referral_event_email     if params[:cte_id]       usermailer.delay.send_referred_message(@user)     end   end end 

i'm using gorails.com fb omniauth tutorial build this.

here's user model db

 create_table "users", force: :cascade |t|     t.string   "email",                  default: "",    null: false     t.string   "encrypted_password",     default: "",    null: false     t.string   "reset_password_token"     t.datetime "reset_password_sent_at"     t.datetime "remember_created_at"     t.integer  "sign_in_count",          default: 0,     null: false     t.datetime "current_sign_in_at"     t.datetime "last_sign_in_at"     t.string   "current_sign_in_ip"     t.string   "last_sign_in_ip"     t.datetime "created_at",                             null: false     t.datetime "updated_at",                             null: false     t.string   "referral_id"     t.string   "first_name"     t.string   "last_name"     t.string   "phone_number"     t.boolean  "redeemed",               default: false     t.string   "dealership"     t.integer  "points",                 default: 0     t.boolean  "referral_completed",     default: false     t.string   "name"     t.index ["email"], name: "index_users_on_email", unique: true     t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true   end 

thanks help!

the problem if user present (i.e. second time), there no call redirect user go root or somewhere.

you have if-else block check whether or not user visiting application first time. if user visits first-time, creating user , redirecting user using

sign_in_and_redirect user, event: :authentication 

however, if user present (i.e. if block), there no redirect call. default, rails therefore trying find view template facebook.html.erb, not present. hence, error

users::omniauthcallbackscontroller#facebook missing template request format , variant. request.formats: ["text/html"] request.variant: [] note! xhr/ajax or api requests, action respond 204 no content: empty white screen. since you're loading in web browser, assume expected render template, not nothing, we're showing error extra-clear. if expect 204 no content, carry on. that's you'll xhr or api request. give shot. 

you can add redirect call @ end of if block

 if service.present?       user = service.user       service.update(         expires_at: time.at(auth.credentials.expires_at),         access_token: auth.credentials.token,       )       #add following line       sign_in_and_redirect user, event: :authentication  else       .... 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -