I have started on a web application for user registration etc. using devise gem. I am novice to Ruby/Rails env. So this is part of my training.
My question is very similar to an old posting # devise overriding registrations controller - uninitialized constant Users::RegistrationsController
After the homepage displays, when I click on signup button, I get this error. I have done some research on this issue on the web to no avail.
In app/controllers/users/registrations_controllers.rb I have this code:
class Users::RegistrationsController < Device::RegistrationsController
def create
super do |resource|
if params[:plan]
resource.plan_id = params[:plan]
if resource.plan_id == 2
resource.save_with_payment
else
resource.save
end
end
end
end
end
In Routes.rb I have this line of code:
devise_for :users, :controllers => { :registrations => 'users/registrations' }
Please let me know if you need any other information to help resolve this error.
In routes.rb, try:
devise_for :users,
:skip => [:registrations, :sessions]
as user do
# Registrations
get '/signup' => 'users/registrations#new', as: :new_user_registration
post '/signup' => 'users/registrations#create', as: :user_registration
It should work.
Related
My rails application successfully logs out a user and redirects to the about page when in development mode. But the moment i deploy it to production it returns a 302 status and a page that shows "you are being redirected". Am using devise for authentication below is what my code actually looks like.
The routes
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "users/registrations", confirmations: "users/confirmations", sessions: "devise/sessions" }, skip: [:sessions]
devise_scope :user do
get "login" => "devise/sessions#new", as: :new_user_session
post "login" => "devise/sessions#create", as: :user_session
get "/join" => "users/registrations#new", as: :join
get '/logout', to: "devise/sessions#destroy", as: :destroy_user_session
end
resources :companies
get "/about", to: "pages#about"
get "/faq", to: "pages#faq"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'pages#home'
end
My application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
protected
def after_sign_in_path_for(resource)
company_path(resource)
end
def after_sign_out_path_for(resource_or_scope)
"/about"
end
def store_location
session[:return_to] == request.full_path
end
def clear_stored_location
session[:return_to] = nil
end
def redirect_back_or_to(alternate)
redirect_to(session[:return_to] || alternate)
clear_stored_location
end
end
And finally my link to log out.
<%= link_to "Log out", destroy_user_session_path %>
Please note that i am using devise 4.2.0 and capistrano for deployment.
Regards
~
I had to override devise sessions#controller and changed my routes to:
delete 'logout', to: "users/sessions#destroy", as: :destroy_user_session
Then i added delete method to my link for logging out.
I am testing ajax with devise and am running into this error in console:
GET https://example.com/users/get_company?foo=bar 404 (Not Found)
and rails server is returning:
ActionController::RoutingError (uninitialized constant UsersController):
A little bit of code:
config/routes.rb
devise_for :users, :controllers => {registrations: 'registrations'}
devise_scope :user do
authenticated :user do
root 'account#show', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
get 'users/get_company', as: 'get_company'
initializers/devise.rb
config.http_authenticatable_on_xhr = true
config.navigational_formats = ['*/*', :html]
controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
respond_to :json
def get_company
#company = Company.first.name
respond_to do |format|
format.js
end
end
end
In views/devise/registrations there is new.html.erb, get_company.js.coffee and in assets/javascripts there is get_company.js.coffee.
These seem to be working fine because the event happens and then I am getting the routing error.
Any help would be greatly appreciated as I am just learning this to help out with a project. Please ask for any additional code or anything that might help explain the situation better.
Hey your route of get_company is missing /, so it would be:
devise_for :users, :controllers => {registrations: 'registrations'}
devise_scope :user do
authenticated :user do
root 'account#show', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
# 1: If you put your get_company route here, it still works
get '/users/get_company' => 'registrations#get_company', as: 'get_company'
end
# 2: If you put your get_company route here, it works as well
# get '/users/get_company' => 'registrations#get_company', as: 'get_company'
You code will work fine either you put the route in position #1 or #2
Excuse me for this, but I can't find my error.
Here is my registrations_controller.rb code:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
edit_user_registration_path(current_user)
end
end
in my routes:
devise_for :users, :controllers => { :registrations => "registrations" }
And redirection isn't working...
Have you tried:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
'/an/example/path'
end
end
...then adding this to your routes:
devise_for :users, :controllers => { :registrations => "registrations" }
All that is from this, which took about 30 minutes of searching the googles to run across that. Was the only thing that worked for me.
Edit:
I might also add that after poking through the code, it doesn't look like this results in an actual redirect (despite what some docs say); I'm getting a 200 back.
Your path should be valid (try to check rake routes | grep registration) otherwise you will be redirected to root_path or "/"
def signed_in_root_path(resource_or_scope)
scope = Devise::Mapping.find_scope!(resource_or_scope)
home_path = "#{scope}_root_path"
if respond_to?(home_path, true)
send(home_path)
elsif respond_to?(:root_path)
root_path
else
"/"
end
end
from Devise source code
my Devise Recaptcha isn't working correctly because I can skip right over it and still register.
I followed the Devise wiki at - https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise and got everything working accept i encountered the problem above.
Here is my code:
app/controllers/users/registrations.rb
class Users::RegistrationsController < Devise::RegistrationsController
def create
if verify_recaptcha
super
else
build_resource
clean_up_passwords(resource)
flash[:alert] = "There was an error with the recaptcha code below. Please re-enter the code and click submit."
render_with_scope :new
end
end
end
routes.rb
YourApp::Application.routes.draw do
devise_for :users do
root :to => "devise/registrations#new"
get "/" => "devise/registrations#new"
post '/' => 'registrations#new', :as => :new_user_registration
match '/', :to => 'devise/registrations#new'
.
.
.
.
.
end
devise_for :users, :controllers => { :registrations => "users/registrations" }
namespace :user do
root :to => "home#index"
end
config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.public_key = 'mykey123456789'
config.private_key = 'mykey13456789'
end
It is possible it doesn't work because i am in test mode and not on my domain name?
Help would be appreciated!
In environment.rb put the following:
ENV['RECAPTCHA_PUBLIC_KEY'] = 'mykey123456789'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'mykey123456789'
Not being on your domain name may be a problem depending on how you registered the keys.
This was an issue with not having ImageMagick or RMagick installed, had to go through the nonsense to get this working.
These were my trials & tribulations:
ImageMagick - "CORE_RL_magick_.dll not found" or how to install RMagick on windows with ruby 1.9.2
I'm trying to override the create method from the Registrations Controller in Devise to include Recaptcha verification (as seen here and here):
class RegistrationsController < Devise::RegistrationsController
def create
if verify_recaptcha
super
else
build_resource
clean_up_passwords(resource)
flash[:alert] = "Bad words."
render_with_scope :new
end
end
end
Also changed my routes.rb accordingly:
map.devise_for :users, :controllers => {:registrations => "registrations"}, :path_names => {
:sign_up => 'signup',
:sign_in => 'login',
:sign_out => 'logout'
}
When trying to visit the new registration page (with new path name: http://localhost:3000/users/signup) this errors shows up:
LoadError in RegistrationsController#new
Expected /home/benoror/project/app/controllers/registrations_controller.rb to define RegistrationsController
FULL ERROR TRACE
Any help appreciated.
BTW, I'm using Devise 1.0.11 and Rails 2.3.10, thanks!
Is your controller in a Users module? If so, you will need class Users::RegistrationsController and
{:registrations => "users/registrations"}
Edit: According to José Valim, custom controllers don't work prior to Devise 1.1. No reason to be developing on < Rails 3 imho. Sorry I missed that in the original post.