Devise Signout Routing Error - ruby-on-rails

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.

Related

Devise Current User Can't Get Nested Data to Show in View

I'm trying to make an example app to learn about namespace/scopes/modules
Normally I would user current_user helper but I have Client::Addresses nested in behind and would like to grab say the user's city and just display it on their edit page (devise registration/edit screen)
<%= current_user.?? %>
Using the line below. I also added inverse_of as my understanding it'll reverse the relationship as well but no avail.
<%= #user.addresses.cacity %>
I think this is pretty close #user.id works but adding the rest error reads. Looks like I also dealt with strong params just not sure. I'm doing this to practice namespacing:scopes/modules:
undefined method `cacity' for #<ActiveRecord::Associations::CollectionProxy []>
It would be great to do something like.
<%= current_user.addresses.cacity %>
Here's some additional information with what I got so far, let me know if additional info is needed.
routes.rb
Rails.application.routes.draw do
namespace :client do
resources :subscriptions
end
# Security Devise Setup
devise_for :admins
devise_for :users
# Main Pages
root 'website/page#index'
# Client Sections
resources :users do
scope module: "client" do
root :to => 'dashboard#index'
resources :addresses
end
end
namespace :admin do
root :to => 'panel#index'
end
end
user.rb
class User < ActiveRecord::Base
include Gravtastic
gravtastic
# Devise Settings
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
# Model Relationships
has_many :addresses, :inverse_of => :user, class_name: 'Client::Address'
end
client/address.rb
class Client::Address < ActiveRecord::Base
belongs_to :user, :inverse_of => :addresses
end
#user.addresses
is a collection, and you send a (instance) method cacity to a collection, which, as errors states, do not respond to it.
#user.addresses.first.cacity would work.
You could limit the relation to has_one:
has_one :address #...
Which will allow you to use the following:
#user.address.cacity

Devise with a custom model, how would you prevent registrations?

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).

Where is Sign In View located for Devise- Rails 3

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.

NoMethodError in Controller#new

Hi everyone Im working in my app and I run into this error
NoMethodError in GasStationsController#new
undefined method `gas_stations' for #<Class:0x12cf77510>
Rails.root: /Users/Users/Documents/myapps/app1
Application Trace | Framework Trace | Full Trace
app/controllers/gas_stations_controller.rb:4:in `new'
I dont understand if my routes are ok. I put my routes.rb and my GasStationsController
Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :users do
resources :gas_stations
end
....
user_gas_stations GET /users/:user_id/gas_stations(.:format) gas_stations#index
POST /users/:user_id/gas_stations(.:format) gas_stations#create
new_user_gas_station GET /users/:user_id/gas_stations/new(.:format) gas_stations#new
edit_user_gas_station GET /users/:user_id/gas_stations/:id/edit(.:format) gas_stations#edit
user_gas_station GET /users/:user_id/gas_stations/:id(.:format) gas_stations#show
PUT /users/:user_id/gas_stations/:id(.:format) gas_stations#update
DELETE /users/:user_id/gas_stations/:id(.:format) gas_stations#destroy
as you see the method is there "gas_stations"
I only have this on my controller
class GasStationsController < ApplicationController
def new
#user = User.find(params[:user_id])
#gas_station = #user.gas_stations.build
end
end
User model
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 :name, :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
has_many :cars
validates_presence_of :email
end
As the error says, Rails has no idea what gas_stations are as it pertains to a user. You need to set that association:
class User ...
...
has_many :gas_stations
...

Devise and simple-private-messages

I am using Devise in my Ruby on Rails 3 application. I am trying to implement private messaging in my application and I came across this gem:
https://github.com/jongilbraith/simple-private-messages
I (accidentally) ran the following command.
rails generate simple_private_messages:model User Message
It created the Message model. But it changed some properties of my existing User model that I had generated using Devise using the following command:
rails generate devise User
Now, when I start my Ruby on Rails application I get this warning:
[WARNING] You provided devise_for :users but there is no model User defined in your application
And my Devise links have stopped working:
ActionView::Template::Error (undefined local variable or method `edit_user_registration_path' for #<#:0x1064c9490>):
Can someone please suggest how can I integrate the both or revert my changes if it is not possible to use them simultaneously?
I've followed these steps to install the gem:
rails generate devise:install
rails generate devise User
rails generate simple_private_messages:model User Message
Add this line (has_private_messages) to User Model:
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 :email, :password, :password_confirmation, :remember_me
has_private_messages
end
Edit the routes.rb file, the order is important here, devise_for should be defined before the messages routes.
devise_for :users
resources :users do
resources :messages do
collection do
post :delete_selected
end
end
end
If you want the scaffold:
rails generate simple_private_messages:scaffold User Message
And remember to uncomment this (attr_accessor :to):
class Message < ActiveRecord::Base
is_private_message
# The :to accessor is used by the scaffolding,
# uncomment it if using it or you can remove it if not
attr_accessor :to
end

Resources