Situation:
I have an Account model which is a devise model and I'm looking to disable users from being able to register themselves. There is a similar question on SO already but the proposed answer is just not working for me and it also does not have any templates in the answer.
Here's my setup:
registrations/edit.html.erb
<%= form_for resource, as: resource_name, url: account_registration_path(resource_name), html: { method: :patch } do |f| %>
...
<% end %>
I had to change the url: from registration_path to account_registration_path for the form to even render.
The problem is I cannot submit the form without it throwing an error. It errors out with:
ActionController::UnknownFormat in Devise::RegistrationsController#update
routes.rb
devise_for :accounts, skip: [:registrations]
as :account do
get "accounts/edit" => 'devise/registrations#edit', as: 'edit_account_registration'
patch "accounts" => 'devise/registrations#update', as: 'account_registration'
#delete "account" => 'devise/registrations#destroy', as: 'destroy_account_registration'
end
Is it possible to somehow set this up in such a way that the url paths for editing the profile are identical to the ones devise supplies when registrations is not disabled?
I still want users to be able to edit/cancel their account, just not register.
In your devise model (Account), you can remove (or comment out) the registerable plugin from the devise modules section. An example of what it could look like before would be this:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
And after:
devise :database_authenticatable, #:registerable,
:recoverable, :rememberable, :trackable, :validatable
You can read more about that module here.
If you just want the register ability removed, here is another workaround. You can create your own DeviseRegistrationsController, like is demonstrated here, and then in the new action, just set a redirect to the root URL. You can also delete the view (if you have generated them).
Related
Started to learn and develop rails - while it is pretty straight forward I got stuck changing the implementation of the /users/edit view and endpoint in the default configuration of devise.
My approach is pretty simple, all routes and logic for authentication, password resetting etc. get handled by devise and it's routes. My problem is that I want users to change their passwords and email via my custom /settings route and view. For me /users/edit just doesn't make sense and I want to remove it from the accessible routes and ideally use its logic for my custom route.
routes.rb
Rails.application.routes.draw do
devise_for :users
# Defines the root path route ("/")
root "home#index"
# SETTINGS
get "settings/profile" => "settings#profile"
get "settings/account" => "settings#account"
get "settings/billing" => "settings#billing"
get "settings/notifications" => "settings#notifications"
end
user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :lockable,
:trackable, :omniauthable
def remember_me
true
end
end
settings_controller.rb
class SettingsController < ApplicationController
before_action :authenticate_user!
def profile
end
def account
end
def billing
end
def notifications
end
end
I have integrated devise in a pre-existing "User" Model. Here it is how it looks like
User.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
include AstroModels::Users::Validation
when i browse any authentication page. i get following error
ActionView::Template::Error (undefined method `[]' for nil:NilClass):
I have debug it using <%resource.inspect%> , i get following error,
<#User not initialized>
on the following line
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
Also, i tried a stack overflow answer by changing
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
to
**<%= form_for(resource_name, :as => resource, :url => session_path(resource_name)) do |f| %>**
But doesn't seem work. Just for info i'm integrating devise in an application that uses rails4.
For just more iinfor i have following in my application helper file.
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
Any suggestion/workaround would be great help thanks.
In my case i was inheriting my custom User Model with ActiveRecord::Base class and by ActiveRecord::Base when we inherit any class with it we can not have our own initialize method in that custom model. So i commented out initialize method in my custom model and the error was gone.
Hope it will help you.
You didn't mention routes.rb or the views you need to create a session, which initializes user. Have you completed those tasks? You should start with the Devise installation generator to add in the code including initializers:
rails generate devise:install
routes.rb has to include devise_for that is set for your application. An example, not necessarily complete for you:
Sampleapp::Application.routes.draw do
root :to => "home#index"
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users
end
You can access the views in the Devise gem, or you can copy them to your own application so they can be customized like this:
rails generate devise:views
This will provide app/views/devise views as well as app/views/layouts needed to login and create a session, among others.
This might be a stupid question, but my Sign In view looks like total crap and isn't roping in any of the CSS styles from the other pages.
For whatever reason, I cannot find this view to even edit it. The only folders in my views are posts, pages, and layout. Does anyone know how I can go about editing what the sign in view looks like?
my route.rb:
Projectmadrone::Application.routes.draw do
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
devise_for :users
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
resources :posts
user model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :posts
end
You need to run the generator for devise views, this will copy them to your app (they live in the gem by default):
rails g devise:views
There's more info about configuring the views here: https://github.com/plataformatec/devise#configuring-views
Make sure you use and leverage and style the views provided for you for users to register and edit their accounts. See app/views/devise/registrations Don't write your own. Use these provided ones.
I followed this guide step by step: http://railscasts.com/episodes/241-simple-omniauth
When I click the button that would take me on twitter, the result is this:
No route matches [GET] "/auth/twitter"
I made several attempts, watching the video in slow motion ... but the result is always the same
In the future, try to share your relevant code for debugging purposes. However, make sure you have the following.
In your routes, make sure you have something like
devise_for :users, :controllers => {:omniauth_callbacks => "users/omniauth_callbacks" ...
and in your, devise initializer
config.omniauth :facebook, facebook_app_id, facebook_app_secret,
{ :scope => 'yourscopeshere', :client_options => {:ssl => {:ca_path => "/etc/ssl/certs"}} }
You need to comment out ':omniauthable' in your model used by the Devise gem (usually it's the model 'User' = the user.rb file):
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable # plus whatever other calls...
# :omniauthable
[...]
end
Using the ':omniauthable' call means loading devise/omniauth components (which cause conflicts with your omniauth setup).
I'm using Devise 1.5.1 in a Rails 3.0.3 app. It works well, with one exception: The signout link gives me this error:
Routing Error
uninitialized constant UsersController
The link that leads to this is:
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
I haven't created an app/controllers/user_controller.rb file, but my understanding that this wasn't necessary when using Devise, correct?
In case it's relevant, my routes.rb file looks like:
Su::Application.routes.draw do
get "group/create"
devise_for :users
resources :users
resources :payers
resources :payments
resources :categories
resources :groups
match "adduser", :to => "groups#adduser"
root :to => "pages#home"
end
...and app/models/user.rb looks like:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :email, :password, :password_confirmation, :remember_me, :group_id
end
I have googled and searched on SO extensively, but to no avail. How should I troubleshoot something like this?
In your routes file, you have
devise_for :users
which serves for the routes for Devise, but
resources :users
is a generic CRUD route, which makes Rails to think that in your app, you have Users Controller, and that you are doing something with the Users model in your model.
The error tells that you don't have a Users Controller and that's true, but it's looking for it because of the route.
So, either delete the line or add a Users Controller if you want to do something with the Users model.
If anything is not clear, post it as a comment.