I'm having a problem creating an Engine in Rails.
I want to create an engine that uses Devise to share over all my other applications.
I followed the Getting Started guide for Engines at rails website:
http://edgeguides.rubyonrails.org/engines.html
Created an application, required my engine... everything runs just fine.
Then, I run into Devise (I've already installed Devise direct in an application before, but not in an engine) but when I try to use it, by acessing any controller that inherits the before_action authenticate_user! I get this message:
http://i.imgur.com/dIIxgwU.jpg
I really don't understand (I'm starting in Ruby, I'm a CakePHP guy), the route is set, but it can't find it (?).
engine/config/initializers/devise.rb
Devise.setup do |config|
...
config.router_name = :doisbit
config.parent_controller = 'Doisbit::ApplicationController'
end
engine/config/routes.rb
Doisbit::Engine.routes.draw do
...
devise_for :users, class_name: "Doisbit::User", module: :devise
...
end
engine/lib/doisbit/engine.rb
module Doisbit
class Engine < ::Rails::Engine
isolate_namespace Doisbit
end
end
engine/lib/doisbit.rb
require 'doisbit/engine'
require 'devise'
...
engine/doisbit.gemspec
...
Gem::Specification.new do |s|
...
s.add_dependency "devise"
end
engine/app/controllers/application_controller.rb
module Doisbit
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
end
application/config/routes.rb
Rails.application.routes.draw do
...
mount Doisbit::Engine, at: "/doisbit"
end
application/config/environments/development.rb
Rails.application.configure do
...
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
end
application/Gemfile
...
gem 'doisbit', path: "C:/.../doisbit"
...
EDIT
Just noticed by accessing /doisbit/users/sign_in I get the form. (duh)
i.imgur.com/anTRK2d.jpg
But... still... by default it routes to /users/sign_in when i'm not logged in.
What I'm missing?
Related
I would like to secure the rails_admin pages using the sorcery gem. According to this SO answer, the way to do this is as follows:
# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
config.authenticate_with do
# Use sorcery's before filter to auth users
require_login
end
end
# app/controllers/application_controller.rb
class ApplicationController
# Overwrite the method sorcery calls when it
# detects a non-authenticated request.
def not_authenticated
# Make sure that we reference the route from the main app.
redirect_to main_app.login_path
end
end
This overrides sorcery's default method for handling no login. The overriding does work in my app, but when I visit the rails_admin pages, I get the following error:
undefined local variable or method `root_path' for #<RailsAdmin::MainController.
so the overriding is not working in the rails_admin code. I am mounting rails_admin at the bottom of my routes file with
# config/routes.rb
...
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
How do I fix this?
It is because the rails_admin controller is not inheriting from my application controller. There is a rails_admin configuration setting that sets this inheritance, i.e.
#config/initializers/rails_admin.rb
RailsAdmin.config do |config|
...
config.parent_controller = 'ApplicationController'
end
I'm getting the following error message when trying to access the admin/admin_users route provided by the Administrate gem: LoadError in Admin::AdminUsersController#index. The other admin routes (i.e. Users, Topics, Posts) work fine. I haven't changed any of the other default configuration for Administrate.
I encountered the bug while working on a code walkthrough available here: https://rails.devcamp.com/professional-rails-development-course/advanced-user-features/customizing-forms-administrate-dashboard. The URL contains a link to the repo; my local version is identical aside from using rails 5.
Portions of relevant files are included below. Any idea what might be causing this error?
localhost:3000/admin/admin_users
Unable to autoload constant Admin::AdminUsersController, expected .../app/controllers/admin/admin_users_controller.rb to define it
...
else
require_or_load(expanded, qualified_name)
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined? (const_name, false)
return from_mod.const_get(const_name)
end
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
../app/controllers/admin_user_controller.rb
module Admin
class ApplicationController < Administrate::ApplicationController
end
end
../config/routes.rb
Rails.application.routes.draw do
namespace :admin do
resources :users
resources :topics
resources :admin_users
resources :posts
root to: "users#index"
end
...
../vendor/gemfile.rb
...
gem "administrate", "~> 0.3.0"
gem 'bourbon'
I am building a Rails application (using ActiveAdmin and Devise) and I am trying to override the after_sign_up_path_for to change the redirection after signing up.
I followed this tutorial from devise but my RegistrationsController is never called. I guess it might work a little bit differently with ActiveAdmin.
I also tried other solution I found on stack overflow without any luck.
Here is my routes:
Rails.application.routes.draw do
devise_config = ActiveAdmin::Devise.config
devise_config[:controllers][:omniauth_callbacks] = 'users/omniauth_callbacks'
devise_config[:controllers][:registrations] = 'registrations'
devise_for :users, devise_config
ActiveAdmin.routes(self)
# other routes
end
And my RegistrationsController: (which is never called)
class RegistrationsController < ActiveAdmin::Devise::RegistrationsController
protected
def sign_up(_resource_name, _resource)
true
end
def after_sign_up_path_for(_resource)
root_url
end
end
Thanks for your help !
My project:
Rails 4.2.6
ActiveAdmin 1.0.0.pre2
Devise 3.5.9
ActiveAdmin don't use your RegistrationsController and can't use them. You can define that method on your ApplicationController or you can do it this way:
# conig/initializer/active_admin.rb
ActiveAdmin::Devise::RegistrationsController.class_eval do
def after_sign_up_path_for(_resource)
root_url
end
end
I'm trying to install the Rails Admin Gem using Sorcery for authentication instead of Devise.
Rails admin does provide a hook that you can use to attach your own authentication method. Here is the example they provide in their docs (using warden):
config.authenticate_with do
warden.authenticate! :scope => :admin
end
config.current_user_method { current_admin }
I'm guessing that inside the block I need to reference the before_filter that Sorcery uses to authenticate users, which would be require_login.
However, when I try that and I try to visit /admin when logged out, I get a routing error:
No route matches {:action=>"new", :controller=>"sessions"}
This probably happens because I am being redirected within the engine rather than in the main app.
How can I set this up correctly?
# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
config.authenticate_with do
# Use sorcery's before filter to auth users
require_login
end
end
# app/controllers/application_controller.rb
class ApplicationController
# Overwrite the method sorcery calls when it
# detects a non-authenticated request.
def not_authenticated
# Make sure that we reference the route from the main app.
redirect_to main_app.login_path
end
end
#config/initializers/rails_admin.rb
RailsAdmin.config do |config|
...
config.parent_controller = 'ApplicationController'
end
If you use Sorcery with Cancancan gem, you should also add config.current_user_method(&:current_user) in your config/initializers/rails_admin.rb file, or you'll get the error: You are not authorized.
I started using comatose to handle content on my site but am having problems using it with my existing authentication using a generic Authlogic config.
In the readme he sites an example for configuring it with Restful Authentication and I'm wondering how I would do the same within a general Authlogic setup?
#environment.rb
Comatose.configure do |config|
# Includes AuthenticationSystem in the ComatoseController
config.includes << :authenticated_system
end
http://github.com/darthapo/comatose
I think a better way to do it is moving the auth methods out into a module, and include that from both ApplicationController and comatose. Example:
Put your auth methods into user_sessions_helper:
module UserSessionsHelper
module AuthMethods
private
...
def require_user
...
Then include the module in your ApplicationController:
class ApplicationController < ActionController::Base
include UserSessionsHelper::AuthMethods
...
And finally in the comatose config as well (environment.rb):
Comatose.configure do |config|
config.admin_includes << "UserSessionsHelper::AuthMethods"
config.admin_authorization = :require_user
end