Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `destroy_user_session_path' for #4220

Closed
wolfieorama opened this issue Nov 27, 2015 · 33 comments
Closed

undefined method `destroy_user_session_path' for #4220

wolfieorama opened this issue Nov 27, 2015 · 33 comments

Comments

@wolfieorama
Copy link

I have installed activeadmin but when i navigate to the localhost:3000/admin i get an erro of a missing method, where should this be defined

undefined method `destroy_user_session_path' for<ul class="header-item tabs" id="utility_nav"></ul> :ActiveAdmin::Views::TabbedNavigation

here is my activeadmin.rb

ActiveAdmin.setup do |config|

  config.site_title = "Sama Tracker"

  def authenticate_user!
    if !current_user.admin?
     redirect_to new_user_session_path
    end
  end

  config.authentication_method = :authenticate_user!
  config.current_user_method = :current_user
  config.logout_link_method = :delete
  config.logout_link_path = :destroy_user_session_path
  config.batch_actions = true
  config.authorization_adapter = ActiveAdmin::CanCanAdapter
  config.localize_format = :long

end

Help please

@timoschilling
Copy link
Member

did you have installed and setup devise? Please post you routes.rb and user model

@wolfieorama
Copy link
Author

I am not using devise , I am using Cancancan and sorcery

here is routes.rb

Rails.application.routes.draw do
  ActiveAdmin.routes(self)
  get 'projects/index'

  resources :password_resets, only: [:new, :create, :edit, :update]

  resources :domains, only: [:index, :create, :edit, :update, :destroy]
  resources :projects, only: [:index, :create, :edit, :update, :destroy]
  resources :tasks, only: [:index, :create, :edit, :update, :destroy]
  resources :fact_pages, only: [:index, :create, :edit, :update, :destroy]
  resources :task_types, only: [:index, :create, :edit, :update, :destroy]

  namespace :reports do
    get 'all'
    get 'production'
    get 'non_production'
    get 'daily_reports'
  end

  resources :activities, only: [:create, :edit, :update, :destroy] do
    collection do
      post :start
      get :stop
      get :export
    end
  end

  root to: 'dashboard#index'

  resources :users do
    collection do
      get :export
    end
    member do
      get :activate
      patch :confirm_activate
    end
  end
  resources :dashboard, only: [:index]
  resources :user_sessions, only: [:new, :create, :destroy]

end


and user.rb 


equire 'model_sync'

class User < ActiveRecord::Base
  include ::ModelSync

  paginates_per 50

  authenticates_with_sorcery!
  acts_as_paranoid

  enum status: { Active: 0, Terminated: 1, Resigned: 2 }
  enum level: { Admin: 0, Regular: 1, Client: 2 }
  enum contributor: { Data_Entry_Contributor: 0, Judgment_Contributor: 1, Contributor_Lead: 2 }
  enum shift: { Day: 0, Night: 1 }

  has_many :activities
  has_many :domains
  has_many :fact_pages
  has_many :tasks
  has_many :task_types
  has_many :projects

  validates :email, presence: true, uniqueness: true, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :status, presence: true
  validates :level, presence: true
  validates :contributor, presence: true

  before_create :setup_activation
  after_create :send_activation_needed_email!

  def self.to_csv(options={})
    CSV.generate(options) do |csv|
      csv << column_names_except_password_fields
      all.each do |user|
        csv << csv_output_row(user)
      end
    end
  end

  def self.column_names_except_password_fields
    column_names - ['crypted_password', 'remember_me_token', 'remember_me_token_expires_at', 'reset_password_token', 'reset_password_token_expires_at', 'reset_password_email_sent_at', 'salt', 'deleted_at']
  end

  def self.csv_output_row(row)
    column_names_except_password_fields.map do |field|
      # Necessary to return enum strings instead of enum integers
      row.public_send(field)
    end
  end

  def current_activity
    self.activities.find_by("start_time IS NOT NULL AND end_time IS NULL")
  end

  def to_s
    full_name
  end

  def full_name
    "#{first_name} #{last_name}"
  end

  def active?
    status == 'Active'
  end

  def staff?
    (admin? || client?) && active?
  end

  def admin?
    level == 'Admin' && active?
  end

  def client?
    level == 'Client' && active?
  end

  def sum_of_durations_for_date(activities, date)
    duration_in_secs = activities.where("end_time >= ? and end_time <= ?", date.to_time, (date.to_date + 1).to_time).inject(0.0) { |res, activity| res += activity.duration }
    (duration_in_secs/3600).round(2)
  end

  def production_duration_for_date(date)
    self.sum_of_durations_for_date(activities.production, date)
  end

  def non_production_duration_for_date(date)
    sum_of_durations_for_date(activities.non_production, date)
  end

  def total_hours_for_date(date)
    (production_duration_for_date(date) + non_production_duration_for_date(date)).round(2)
  end

end

@railsfactory-bhaskararao

authenticates_with_sorcery! does this uses devise?

@wolfieorama
Copy link
Author

No it doesnt , its using sorcery gem , is it a must to use devise ? I am using sorcery and Cancancan

@railsfactory-bhaskararao

seems you are missing

devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)

@wolfieorama
Copy link
Author

so does that mean i have to gem install devise , i will end up having to authentication gems .. is that possible , my authentication gem for now is sorcery and not devise

Question: Is it a MUST to have devise installed ? i am guessing now

Or should I just add that line up there in routes ?

@railsfactory-bhaskararao

yes it is must to have devise installed. Because activeadmimn uses devise for authentication.

@wolfieorama
Copy link
Author

thanks

and can i have devise and sorcery side by side ? sorcery to authenticate in my app and devise for active admin ? then cancan adapter for authorization

@railsfactory-bhaskararao

yes, as they works as expected.

@wolfieorama
Copy link
Author

ok thanks a lot !! let me give it a shot .... in case i am stuck i will give you a shout

@railsfactory-bhaskararao

Thanks, I can hear that!! 😄

@timoschilling
Copy link
Member

@wolfieorama you don't need to use devise!

@wolfieorama
Copy link
Author

@timoschilling so whats my way out ?

@timoschilling
Copy link
Member

@wolfieorama If you don't use devise, you need to set config.logout_link_path to a path helper that returns the logout url and maybe you need to change the config.logout_link_method too

@wolfieorama
Copy link
Author

ok , that sounds better that running 2 auth gems .....

do you a guide example on how to create that .. sample may or a quick guide :)

@timoschilling
Copy link
Member

How do you lock out with sorcery?

@wolfieorama
Copy link
Author

you mean logout ?

@wolfieorama
Copy link
Author

this is the route: user_session DELETE /user_sessions/:id(.:format) user_sessions#destroy

def destroy
    authorize! :log_out, User
    logout
    redirect_to root_url
end

this is in user_session_controller.rb

@timoschilling
Copy link
Member

then you need to use config.logout_link_path = :user_session

@wolfieorama
Copy link
Author

same error:
"undefined method `user_session' for


    :ActiveAdmin::Views::TabbedNavigation

    config.logout_link_method = :destroy or :delete - i have tried both
    config.logout_link_path = :user_session

    @timoschilling
    Copy link
    Member

    sorry config.logout_link_path = :user_session_path

    @wolfieorama
    Copy link
    Author

    draaaaat !! how did i not see that ..... sleepy eye :).... a bit of progress

    new error :

    No route matches {:action=>"destroy", :controller=>"user_sessions"} missing required keys: [:id]

    should i include the :id in params ... i dont think so

    @timoschilling
    Copy link
    Member

    @wolfieorama
    Copy link
    Author

    got you .. my bad @timoschilling thanks

    @timoschilling
    Copy link
    Member

    ActiveAdmin don't support urls with ids at logout path at the moment.

    @wolfieorama
    Copy link
    Author

    oh no ....any workarounds possible ?.... or does that mean i use devise for activeadmin authentication and sorcery for the rails app ?

    @wolfieorama
    Copy link
    Author

    @timoschilling is it ok to use the 2 gems side by side ? it doesnt feel good to me .... but might give it a try

    @timoschilling
    Copy link
    Member

    You can refactor your sign out action to work with the current_user instead of the user id.

    @timoschilling
    Copy link
    Member

    But yes, working with 2 gems is ok, but is not necessary.

    @wolfieorama
    Copy link
    Author

    i would go with the first option ..... thanks a lot any example available using either methods ?

    @timoschilling
    Copy link
    Member

    you can say active admin to link to logout_link_method and the rest is yours

    @wolfieorama
    Copy link
    Author

    👍

    @wolfieorama
    Copy link
    Author

    @railsfactory-bhaskararao on your suggested method above where i have to use both devise and sorcery i have successfully managed to load the activeadmin page but on signing in i get and error:

    undefined method `admin?' for #AdminUser:0x007faecb687618

    i then went ahead and created the method in admin_user.rb

    def admin?
         @admin = AdminUser
       end
    
       def active?
         @active = AdminUser
       end

    this now leads to another error:

    This action failed the check_authorization because it does not authorize_resource. Add skip_authorization_check to bypass this check.

    any tips , also i am wonderin in application controller can i have check authorization unless: for both devise controller and activeadmin resource

    thanks in advance

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants