Can I use "devise_for", with "only" and "excepted"? - ruby-on-rails

I'm building a Rails website, and setting up the devise routes. So far, I have no trouble and everything works. However, since the website is divided between the administration and the visitor place, I would like the user to be able to edit his profile from a path that looks like /admin/users/1/edit and sign in/out from /user/sign_in or user/sign_out (without the admin prefixe).
So far, I managed to do either with or without the prefix, but not both at the same time.
Here is the relevant part of my route file:
devise_for :users, only: %w['session#new session#destroy']
get '/admin' => 'home#admin', as: :admin
authenticate :user do
scope '/admin' do
resource :basics
resources :portfolios
resources :articles
devise_for :users, excepted: %w['sessions#new session#destroy']
end
end
And here is the result of rake routes:
new_user_session GET /admin/users/sign_in(.:format) devise/sessions#new
user_session POST /admin/users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /admin/users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /admin/users/password/new(.:format) devise/passwords#new
edit_user_password GET /admin/users/password/edit(.:format) devise/passwords#edit
user_password PATCH /admin/users/password(.:format) devise/passwords#update
PUT /admin/users/password(.:format) devise/passwords#update
POST /admin/users/password(.:format) devise/passwords#create
cancel_user_registration GET /admin/users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /admin/users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /admin/users/edit(.:format) devise/registrations#edit
user_registration PATCH /admin/users(.:format) devise/registrations#update
PUT /admin/users(.:format) devise/registrations#update
DELETE /admin/users(.:format) devise/registrations#destroy
POST /admin/users(.:format) devise/registrations#create
How can do this?
Thank you in advance

I believe it may just be a typo, but try changing:
devise_for :users, excepted: %w[sessions#new session#destroy]
to:
devise_for :users, except: %w[sessions#new session#destroy]

Related

Routing Error when browsing an instance

Routing Error
No route matches {:action=>"edit", :controller=>"statuses", :id=>nil}
When http://0.0.0.0:3000/users/2
Dont understand why it says :controller=>"statuses" when my route file says:
Treebook::Application.routes.draw do
get "users/show"
resources :credits
resources :merchants
devise_for :users
resources :statuses
root to: 'statuses#index'
get 'users/:id', to: 'users#show'
Rake Route
root / statuses#index
credits GET /credits(.:format) credits#index
POST /credits(.:format) credits#create
new_credit GET /credits/new(.:format) credits#new
edit_credit GET /credits/:id/edit(.:format) credits#edit
credit GET /credits/:id(.:format) credits#show
PUT /credits/:id(.:format) credits#update
DELETE /credits/:id(.:format) credits#destroy
merchants GET /merchants(.:format) merchants#index
POST /merchants(.:format) merchants#create
new_merchant GET /merchants/new(.:format) merchants#new
edit_merchant GET /merchants/:id/edit(.:format) merchants#edit
merchant GET /merchants/:id(.:format) merchants#show
PUT /merchants/:id(.:format) merchants#update
DELETE /merchants/:id(.:format) merchants#destroy
statuses GET /statuses(.:format) statuses#index
POST /statuses(.:format) statuses#create
new_status GET /statuses/new(.:format) statuses#new
edit_status GET /statuses/:id/edit(.:format) statuses#edit
status GET /statuses/:id(.:format) statuses#show
PUT /statuses/:id(.:format) statuses#update
DELETE /statuses/:id(.:format) statuses#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user GET /users/:id(.:format) users#show
get "users/show" - that will probably be causing your problem
Here's a better routes file for you:
root to: "statuses#index"
resources :credits, :merchants, :statuses
devise_for :users
resources :users, only: [:show]
Aggggg
The problem was on some footer links that I copy pasted. I just deleted both links since I dont need them. Incredible how can your attention gets stuck in a point far away from the root problem.
|

route in rake routes but page says No route matches [GET] with devise

I have added custom routes for the devise actions. this does not work when I try to go to /profile/edit or /login or /logout Here is the rake routes:
saasbook#saasbook:~/Documents/ronde$ rake routes
static_about GET /static/about(.:format) static#about
static_tour GET /static/tour(.:format) static#tour
static_home GET /static/home(.:format) static#home
static_terms_of_use GET /static/terms_of_use(.:format) static#terms_of_use
static_contact GET /static/contact(.:format) static#contact
users_profile GET /users/profile(.:format) registrations#edit
login GET /login(.:format) devise/sessions#new
logout GET /logout(.:format) devise/sessions#destroy
register GET /register(.:format) devise/registrations#new
profile_edit GET /profile/edit(.:format) devise/registrations#edit
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session GET /users/sign_out(.:format) devise/sessions#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) omniauth_callbacks#passthru {:provider=>/google|facebook/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) omniauth_callbacks#(?-mix:google|facebook)
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root / static#home
Here is my routes.rb file where I added the the new routes with the same controller actions for devise:
Ronde::Application.routes.draw do
get "static/about"
get "static/tour"
get "static/home"
get "static/terms_of_use"
get "static/contact"
get "/user/profile", :to => 'registrations#edit'
get "/login", :to => "devise/sessions#new" # Add a custom sign in route for user sign in
get "/logout", :to => "devise/sessions#destroy" # Add a custom sing out route for user sign out
get "/register", :to => "devise/registrations#new" # Add a Custom Route for Registrations
get "profile/edit", :to => "devise/registrations#edit"
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
root to: 'static#home'
end
# :path_names => {:edit => "profile/edit", :sign_in => "login", :sign_out => "logout", :sign_up => "register" }
Then page says that and should route to the devise controller:
Routing Error
No route matches [GET] "/profile/edit"
Try running rake routes for more information on available routes.
I don't know if this right but I think you can't do that way, because Devise doesn't have any controller. Please check this question and this.
What I done usually was making another controller for Devise, or normal controller like users_controller with it own views. And register it on routes.rb like:
devise_for :users
scope "/admin" do
resources :users
end
Then when I need to open it, I called localhost:3000/admin/users
But please correct me if there anything wrong, or my way to do it was wrong. Hope can help.

devise redirect on login error: ActiveRecord::RecordNotFound in DocumentsController#show

I 'm trying to redirect to a specific page after sign_in & sign_up. I have two models users & resources. After sign_in I want to show list of documents that belong to the user.
If there are no documents then it should redirect to users/:id/document#create otherwise users/:id/document#index.
Any suggestions how do i do that?
I get the following error for the redirect path defined in my controller.
ActiveRecord::RecordNotFound in DocumentsController#show
Couldn't find Document without an ID
Application_controller.rb
def after_sign_in_path_for(user)
user_documents_url(user)
end
Routes.rb
root :to => 'home#index'
devise_for :users
resources :users do
resources :documents
end
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
Routes
root / home#index
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
user_password POST /password(.:format) devise/passwords#create
new_user_password GET /password/new(.:format) devise/passwords#new
edit_user_password GET /password/edit(.:format) devise/passwords#edit
PUT /password(.:format) devise/passwords#update
cancel_user_registration GET /cancel(.:format) devise/registrations#cancel
user_registration POST / devise/registrations#create
new_user_registration GET /sign_up(.:format) devise/registrations#new
edit_user_registration GET /edit(.:format) devise/registrations#edit
PUT / devise/registrations#update
DELETE / devise/registrations#destroy
user_confirmation POST /confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /confirmation/new(.:format) devise/confirmations#new
GET /confirmation(.:format) devise/confirmations#show
user_documents POST /users/:user_id/documents(.:format) documents#create
new_user_documents GET /users/:user_id/documents/new(.:format) documents#new
edit_user_documents GET /users/:user_id/documents/edit(.:format) documents#edit
GET /users/:user_id/documents(.:format) documents#show
PUT /users/:user_id/documents(.:format) documents#update
/user/:user_id/documents(.:format) user/documents#index
Thanks
The code itself has no problem as I see. But you need to build some "documents" data before this test, either by FactoryGirl or stubs.
Because you really don't have "documents" data for any users, of course Rails gave you to RecordNotFound error.
use this method according to following code
def after_sign_in_path_for(resource_or_scope)
// redirect path
end
i hope this will help
I fixed my problem by following this question.Following is my routes.rb
resources :users do
resources :documents, only: [:index]
end
resource :documents, except: [:index]
this gives me document#index path as well as document#new path.

Rails 3.2.1: link works on app, is present in routes.rb but doesn't show up on rake routes output

I have these urls:
http://localhost:3000/assets
http://localhost:3000/assets/new
http://localhost:3000/assets/34
http://localhost:3000/assets/34/edit
they work in my app.
I have this routes.rb file:
devise_for :users
match "listings/show_notes" => "listings#show_notes", :as => :show_notes
resources :users
resources :listings
resources :assets
authenticated :user do
root :to => "listings#index"
end
#this route is for file downloads
match "assets/get/:id" => "assets#get", :as => :download
resources :admin_dash_board, :only => :index
I have the following output when I type in rake routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
show_notes /listings/show_notes(.:format) listings#show_notes
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
listings GET /listings(.:format) listings#index
POST /listings(.:format) listings#create
new_listing GET /listings/new(.:format) listings#new
edit_listing GET /listings/:id/edit(.:format) listings#edit
listing GET /listings/:id(.:format) listings#show
PUT /listings/:id(.:format) listings#update
DELETE /listings/:id(.:format) listings#destroy
root / listings#index
admin_dash_board_index GET /admin_dash_board(.:format) admin_dash_board#index
As you can see there are no routes for the resource :assets.
Any idea why? Or whats going on?
Thanks
I think I found your answer here and here.
Try to add this line in your application.rb:
config.assets.prefix = "/new_route"
EDIT - also here with some other options to solve the problem....

User Profile Controller Rspec Test Fails due to route

I'm having a problem testing my new user profile page with Rspec. It's working through the browser, but Rspec is blowing up.
I'm using devise with a separate controller for editing profile fields.
This is a request spec.
it 'Shows the user profile with their non-private data' do
visit user_path(#user)
page.should have_content #user.full_name
end
Fails with this error:
Failure/Error: visit user_path(#user)
ActionController::RoutingError:
No route matches {:action=>"show", :controller=>"users"}
I disagree
#routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users, :only => [:edit, :update, :show]
The update and edit path helpers are working just fine.
The controller action:
class UsersController < ApplicationController
layout 'profile', :except => [:show]
#... edit and update omitted
def show
#user = User.find(params[:id])
end
end
rake routes shows:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
So it doesn't really look like devise is interfering with my route, and more importantly, everything works in the browser. What am I missing here?
I also tried making a member action called profile instead of the default show, that had the same result
The problem here was I started a new context block and didn't set up #user before the it block. The route thing was just a red herring.
Also proof that you should walk away from your code for awhile if you can't figure things out.

Resources