0
votes

I'm trying to make an app in Rails 4.

I'm trying to follow this tutorial to setup devise with omniauth.

I have a user model and an profile model. The associations are:

User.rb

has_one :profile

Profile.rb

belongs_to :user

In my omniauth callbacks controller, I have:

def self.provides_callback_for(provider)
    class_eval %Q{
      def #{provider}
        @user = User.find_for_oauth(env["omniauth.auth"], current_user) 

        if @user.persisted?
          sign_in_and_redirect @user,  event: :authentication

          set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
        else
          session["devise.#{provider}_data"] = env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end
    }
  end

In my omniauth callbacks controller, currently, when the user successfully authenticates, the redirect goes to the root path (I'm not sure why). I think it has something to do with the current redirect for @user, not having a show page (which it doesnt - there are no views in my user views folder).

I want to go to the user's profile show page.

I can't figure out how to write this path. I have tried each of:

 if @user.persisted?
              sign_in_and_redirect @user.profile,  event: :authentication

if @user.persisted?
              sign_in_and_redirect @user.profile(profile.id),  event: :authentication

if @user.persisted?
              sign_in_and_redirect @user.profile_id,  event: :authentication

Does anyone know how to make a redirect path. There is a user and the user does have a profile. I"m stuck on how to express the path to the profile's show page.

1

1 Answers

0
votes

In your ApplicationController add

def after_sign_in_path_for(resource)
  profile_path(resource.profile) # or whatever the route is for the destination you want
end

see - http://www.rubydoc.info/github/plataformatec/devise/Devise/Controllers/Helpers:after_sign_in_path_for