device change password form not showing - ruby-on-rails

I googled it but not got solution. I cant see change password form. (edit_user_password_path) it shows the message "you are already logged in" (Most probably flash[:error] and redirects to dashboard page. I am using rails 4 and device 3.5
User is my device model and I am using users controller as well (extend from ApplicationController) as I want to handle CRUD operations on User on my own. I am not using :registerable module of device.
my routes file looks like
devise_for :users, :path => 'u'
resources :users do
get 'manage_resource'
post 'manage_resource'
get 'profile'
end

Add the below snippet to your routes.rb file
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration' end
You should then be able to access edit_user_registration_path
Thanks to https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password

Related

Rails routes based on current user

I followed this rails cast to create authentication for a rails project. My routes currently look like this:
Rails.application.routes.draw do
resources :messages
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
get "new_photo" => "users#edit", :as => "new_photo"
root :to => "users#new"
resources :users
resources :sessions
end
How to I edit this file so that the root will be pointing to "messages#new", if a user is logged in and "users#new" when no user is logged in? I tried many of the solutions on other pages, but they didnt work (they were probably for devise). Thanks for the help!
You'll probably want to handle this in your controller.
routes.rb
root :to => "users#new"
users_controller.rb
def new
return redirect_to new_messages_url if current_user
# normal controller code below...
end
This will redirect the logged_in user (current_user) to the new messages page if already logged in. I'm just assuming that current_user holds your user data, it may be different for your application.
write it in any home controler.
def set_roots
if current_user
redirect_to dashboard_home_index_path
else
redirect_to home_index_path
end
end
in routes.rb file
root :to => 'home#set_roots'
match "/find_roots" => "home#set_roots"

Application Helper Methods Availability

I am using Rails 4 and am trying to include the koudoku stripe gem. Here is my routes:
# Added by Koudoku.
mount Koudoku::Engine, at: 'koudoku'
scope module: 'koudoku' do
get 'pricing' => 'subscriptions#index', as: 'pricing'
end
resource :account
devise_for :users, :skip => [:sessions]
as :user do
get '/login' => 'devise/sessions#new', :as => :new_user_session
post '/login' => 'devise/sessions#create', :as => :user_session
get '/logout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
get '/dashboard', to: 'dashboard#index'
get '/reports/generate', to: 'reports#generate'
authenticated :user do
root :to => 'dashboard#index', :as => :authenticated_root
end
root :to => redirect('/login')
And this is the error I am getting:
undefined local variable or method `root_url
I can access the other routes just fine, it is just trying to render the Application Helper methods (for instance, a custom app method I have defined, or routes methods) from the module routes... Does this make sense? How do I fix this?
Try adding "main_app." before your root path. For example:
main_app.root_path
Conditional logic in the routing layer kind of goes against the intent of the Rails MVC architecture. The route file should just map a web request to a controller, which then has conditional logic to determine what is displayed.
In this case it's a bit different since you want to redirect, but I personally would still put it in the controller. In other words send the root to dashboard#index, and then at the top of that controller (or in a before_filter) just do
redirect_to login_path unless current_user_authenticated?
(here I'm assuming you would have a named route for login, which would be good practice, as well as a current_user_authenticated? method to check whatever logic you want before the redirect. This would be a more Rails-y approach, whatever that's worth...)

Devise: Routes & after_sign_in

I'm overriding the default Devise signin method as such:
def after_sign_in_path_for(resource)
stored_location_for(resource) || jobs_path
end
So that when an authenticated users signs in, they are taken to my jobs page. Which is fine,
but the problem I'm having is that I'd like to be able to utilize the Devise password (new/edit) pages, typically found at /users/password/new and /users/password/edit but when I attempt to go to those locations (http://localhost:3000/users/password/new), I get immediately redirected back to jobs page. What do I need to do to correct this. Below is part of my routes.rb if that helps:
devise_for :users, :skip => [:sessions] do
# devise/sessions
get 'signin' => 'devise/sessions#new', :as => :new_user_session
post 'signin' => 'devise/sessions#create', :as => :user_session
get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
resources :users
Thanks in advance for your time and assistance.
The devise wiki says that users can edit their password using the registerable module:
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password
Have you tried just using devises methods for letting users change their password?
Also, here is a related question:
Rendering the Devise edit Password Form

Devise logs users out after hitting certain routes

Here is my routes files:
Tubalr::Application.routes.draw do
devise_for :users
root :to => "application#index"
get "/player/:search_type/:search/:first_video" => "application#player"
get "/just/:artist_band.json" => "api#just"
get "/similar/:artist_band.json" => "api#similar"
get "/:username/favorites.json" => "api#userFavorites"
get "/just/:artist_band" => "application#index"
get "/similar/:artist_band" => "application#index"
get "/history" => "application#history"
get "/:username/favorites" => "favorites#init"
post "/check-favorites" => "favorites#check"
post "/favorites/add" => "favorites#add"
post "/favorites/remove" => "favorites#remove"
devise_scope :user do
get "/users/sign_out" => "devise/sessions#destroy"
end
end
The routes /history and the default /users/edit route for devise do not log the user out.
I'm not sure what other information to give, if theres something that would help debugging my problem, please let me know and I'll update the question.
The entire projects code can be found here: https://github.com/cjstewart88/Tubalr/tree/user_favorites
After digging around I finally ran into this:
https://github.com/plataformatec/devise/issues/913
It appears that the csrf token needs to be passed along with AJAX request, which my app is doing a good bit.

error setting user_root for devise redirect

Goal
When user submits the devise "edit registration" form, I want to redirect to users#show rather than the site's root.
Problem
Following Devise's instructions has not worked. I don't want to customize the Devise controller, so I'm left with two suggested modifications to routes.rb, either
devise_for :users do
get 'users', :to => 'users#show', :as => :user_root
end
or
match 'user_root' => 'users#show'
The first redirects to http://localhost:3000/users after submitting the edit form, the second redirects to http://localhost:3000/user_root. Both give the same error, "Couldn't find User without an ID".
My users#show page normally works in the app, so it's not an error with the controller method (or view, of course). It seems to be a routing error. I have "resources :users" in my routes.rb file, nothing else regarding users. If I need to give more information please let me know!
Question
Why isn't the user id being passed in the url?
Have you considered making the action a member action? this will ensure that the url format is controller//action. I don't know if you can even use the member thing in the devise route definition, but that's where I'd start.
devise_for :users do
member do
get 'users', :to => 'users#show', :as => :user_root
end
end
The member part doesn't work but if you take that out it is working for me. +1 to jaydel for close enough :P
devise_for :users do
get 'users', :to => 'users#show', :as => :user_root
end
Another thing that I tried which also worked was :
devise_scope :user do
get 'users' => 'users#show', :as => :user_root
end

Resources