change routes in devise - ruby-on-rails-3

Im working with ruby 1.9.2, rails 3.1.3, devise 1.5.3, I need to change the devise routes, I already changed the default routes, like login and logout, but I need to change the others routes, for example:
http://localhost:3000/password/new
I need that, when the user click the link did you forget your password? go to:
http://localhost:3000/recovery_password
in my routes I tried:
get "recovery_password", :to => "devise/passwords#new"
get 'recovery_password' => 'devise_passwords#new', :as => :new_user_password
but don't works, please help.
thanks in advance.

try this. It should works. ;)
# change :devise_model to :users, or :admins or any name of your devise model
devise_for :devise_model do
get 'recovery_password' => "devise/passwords#new"
end
and you can use this in view like this.
link_to 'Forgot you password?', recovery_password_url
PS. if you have customize devise controller. You should tell the router first, and change devise controller to your customize controller name.

Related

Problems signing out with Devise in Rails 4

I'm having problems signing out in my app.
The error that it's giving me is:
Template is missing
Missing template users/destroy, application/destroy with {:locale=> [:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
Searched in: *"/home/gregb/workspace/Sandbox/site/app/views"
* "/home/gregb/.rvm/gems/ruby-2.2.0/gems/devise-i18n-views-0.3.6/app/views"
* "/home/gregb/.rvm/gems/ruby-2.2.0/gems/devise-3.5.2/app/views"
Any help would be greatly appreciated.
If you're using devise, then that doesnt seem to me like the directory where the view exists.
did you do
rails g devise:views
or create your custom views?
there's no destroy.html.erb file in devise.
but if you want to create one , I'd suggest you to create the destroy action in a newly created UsersController, then in the routes you have to place the
resources :users, only: [:destroy]
under the
devise_for :users
in order for it to be accessible. Then rake routes and find the right route. That should create the destroy action route for you.Create views in the app/views/users/destroy.html.erb and that should hopefully do it for you.

Rails 3 with devise undefined method `users_url`

I'm making some small changes to devise in order to overwrite the layout used and some other things so I decided to create my custom routes with controllers. I only added this to the routes so far:
devise_for :users, :controllers => {
:sessions => "sessions/user",
:registrations => "registrations/user"
}
which is supposed to load the login page when accessed by /users/sign_in and it does it very well. When I try to ajax post to that url in order to validate the user input and login I get this error: NoMethodError (undefined method 'users_url' for #<Sessions::UserController:0x000000041bed98>):
The controller is basic and nothing was changed to overwrote devise logic:
class Sessions::UserController < Devise::SessionsController
layout "login" # The only addition. Rest is handled by devise
end
I had this working for a good while but today I decided to use devise for another model (admins) and I generated it's files using the devise command rails generate devise Admin which completely messed my working code of logging in the users.
Any ideas?

Defining own Devise controllers prevents Devise using custom views

I used config.scoped_views = true in initializers/devise.rb to make Devise use my own views rather than its default views for my two roles (Admin and Subscriber). This worked great, and I went ahead and customized my views.
Then earlier today I added my own controllers that subclass Devise's controllers and told devise to use these controllers in routes.rb using:
devise_for :subscribers, :controllers => {
:registrations => "subscriber_registrations",
:sessions => "subscriber_sessions",
:passwords => "subscriber_sessions"
}
This works nicely, however it seems to have had the unwanted side-effect of making Devise revert back to using its default views for my Subscribers role.
In my logs when I load the login page for Subscribers I can see this:
Rendered /Users/myName/.rvm/gems/ruby-1.9.3-p0/gems/devise-2.1.0/app/views/devise/sessions/new.html.erb
Am I doing something wrong? Do I now need to manually add all the view rendering because I've defined my own controllers?

Custom actions in cancan

I have cancan working fine for everything except custom actions. I had an upload_photos action that just wasn't being secured correctly. I added a test action which was really basic that looked like this in the ability.rb file:
can :test, User, :id => user.id
but for some reason it always evaluated to true so I changed it to the following
can :test, User, :email => 'a#a.com'
and again it always evaluates to true.
Anybody know why cancan isn't working correctly here?
My users controller has the following at the top:
load_and_authorize_resource
Thanks in advance.
SOLUTION:
Cancan's load_and_authorize_resource interferes with custom actions so we need to do something like
load_and_authorize_resource :except => [:test]
and then in the test action:
authorize! :test, #user

Using Devise in RoR - How to have only a few pages not require login - Override authenticate_user

For the majority of my site, I want to require login. But for two pages, I need to have a user be able to submit to the page without logging in.
Is there a way to override the
before_filter :authenticate_user!
which I put in the application controller?
In your desired controller, add:
skip_before_filter :authenticate_user!, :only => [:some_action, :another_action]
Read more about filters at Module ActionController::Filters::ClassMethods
before_filter :authenticate_user!, :except => [:action_name_1, :action_name_2]