I have a app using devise to login/out, and view/create profiles. As of yet users may create and delete profiles although there is only supposed to be one profile per user. I have set up my routes just about without problems until comes the SignOut/LogOut from the whole app. Ultimately the error log is all I can decipher, and seems that routes.rb needs some modification for this to work, but I am stumped. Here are the errors and routes.rb:
/log/production.log:
Started DELETE "/users/sign_out" for 127.0.0.1 at 2020-01-26 01:56:53 -0500
ActionController::RoutingError (No route matches [DELETE] "/users/sign_out"):
routes.rb
Rails.application.routes.draw do
devise_for :users, :controllers => {:sessions => "users/sessions" }
resources :profiles, only: [:new, :create, :edit, :update, :destroy]
devise_scope :user do
authenticated :user do
root to: 'profiles#index', as: :authenticated_root
get '/profiles/new' => 'profiles#new'
match '/profiles' => 'profiles#create', via: [:get, :post]
get '/profiles/:id' => 'profiles#show'
get '/profiles/:id/edit' => 'profiles#edit'
match '/profiles/:id' => 'profiles#update', via: [:get, :post]
delete '/profiles' => 'profiles#destroy', via: [:get, :post]
end
unauthenticated :user do
root to: 'devise/sessions#new', as: :unauthenticated_root
match '/users/sign_in' => 'devise/sessions#create', via: [:get, :post]
delete '/users/sign_out' => 'devise/sessions#destroy'
end
end
end
I read that using resources :users may affect devise sessions controller, in that I would need a UsersController, however haven't included resources :users in my routes, and/or for a similar error.
You placed the sign-out route in the unauthenticated block in your routes.rb
unauthenticated :user do
# ..
delete '/users/sign_out' => 'devise/sessions#destroy'
end
What doesn't makes sense, only authenticated users can sign out. Just move that method into the authenticated :user block above:
authenticated :user do
# ..
delete '/users/sign_out' => 'devise/sessions#destroy'
end
Related
Locally my password reset through 'forget password' link works fine. In production I receive the mail but it states that the route is not there. I however resources :password_resets in my routes?
Full routes
Rails.application.routes.draw do
get 'password_resets/new'
devise_for :users, controllers: { registrations: "registrations" }
root to: 'pages#home'
resources :companies, except: :index do
resources :messages
member do
put "like" => "companies#upvote"
put "unlike" => "companies#downvote"
end
end
resources :messages, only: [:index, :show]
resources :contacts, only: [:new, :create]
resources :password_resets
get '/profile', to: "users#profile", as: :profile
get '/show', to: "users#show", as: :show
get '/overview', to: "companies#overview", as: :overview
get '/about', to: "pages#about"
get '/how-does-it-work', to: "pages#how_does_it_work"
# mount using default path: /email_processor
mount_griddler
get '/send-messages', to: "messages#send_messages", as: :sendmessages
delete 'avatar' => 'users#delete_avatar', as: :delete_avatar
# Terms of Service and Privacy Policy
get '/terms-of-service', to: "pages#terms_of_service"
get '/privacy-policy', to: "pages#privacy_policy"
get '/cookies', to: "pages#cookies"
get '/companies', to: "userselections#select", as: :select
# --- User selections routes ---
get '/user_selections', to: "userselections#select", as: :user_selections
post '/user_selections', to: "userselections#create", as: :new_user_selections
delete '/user_selections/:id', to: "userselections#destroy", as: :destroy_user_selections
end
Error I get on our staging
No route matches [GET] "/password_resets/ubVUvWvkqG60kU1S5jTvuw/edit"
I am running my rails app as sub-directory (for apache mapping reasons):
config.ru:
map '/sub-directory' do
run Rails.application
end
application.rb:
config.root_directory = "/sub-directory/"
config.action_controller.relative_url_root = '/sub-directory'
so now my app (it's root) is ran from the route http://localhost:3000/sub-directory. The problem is when I go to http://localhost:3000/ I get the page:
Not Found: /
I understand I am getting that because my app isn't running in there anymore but I would like that route to redirect to http://localhost:3000/sub-directory. Is there anyway I can accomplish this?
if this helps, routes.rb:
require 'resque/server'
Rails.application.routes.draw do
get "handle/:handle" => "handles#index"
get "/tag/:hashtag" => "tags#index"
get "handles/:handle" => "handles#index"
get "/tags/:hashtag" => "tags#index"
devise_for :users, :controllers => {
:registrations => "registrations",
:passwords => "passwords"
}
devise_scope :user do
get "/email" => "registrations#email"
post "/registrations/check_email" => "registrations#check_email"
get "/users/sign_in" => "registrations#signup"
end
authenticated :user do
devise_scope :user do
root :to => redirect("app"), as: :authenticated_root
get "/app", :to => 'reports#index'
end
end
unauthenticated do
devise_scope :user do
root to: 'registrations#new'
end
end
get '/oauth' => 'social_platforms#new'
get "/auth/:provider/callback" => "social_platforms#create"
get '/login', :to => 'sessions#new', :as => :login
# match '/auth/failure' => 'sessions#failure', :via => [:get]
# get '/reports', to: 'pages#reports', as: :reports
resources :reports, only: [:show, :index]
get '/get_report', to: 'reports#get_report'
get '/check_token', to: 'reports#check_token'
mount Resque::Server.new, at: "/resque"
get '*path' => redirect('/')
end
I'm trying to split my rails project in a front-end for regular users and a back-end for admins. Therefore i have created a namespace 'admin' so that i can easily control admin.After creating the admin namespace the I changed the routes from
Rails.application.routes.draw do
authenticated :user do
root to: 'dashboard#index', as: :authenticated_root
end
unauthenticated do
root to: "home#index"
end
match '(errors)/:status', to: 'errors#show', constraints: { status: /\d{3}/ }, via: :all
devise_for :users, skip: [:registrations]
as :user do
get 'my/profile/edit' => 'devise/registrations#edit', as: 'edit_user_registration'
patch 'my/profile' => 'devise/registrations#update', as: 'user_registration'
end
resources :users
resources :events do
patch :archive, :unarchive
end
end
to this
Rails.application.routes.draw do
namespace :admin do
authenticated :user do
root to: 'dashboard#index', as: :authenticated_root
end
unauthenticated do
root to: "home#index"
end
match '(errors)/:status', to: 'errors#show', constraints: { status: /\d{3}/ }, via: :all
devise_for :users, skip: [:registrations]
as :user do
get 'my/profile/edit' => 'devise/registrations#edit', as: 'edit_user_registration'
patch 'my/profile' => 'devise/registrations#update', as: 'user_registration'
end
resources :users
resources :events do
patch :archive, :unarchive
end
end
end
After these change I got this page
Rails::WelcomeController#index as HTML
Does anyone know how to do this?
If I understand what you ask, you want to put everything admin related into the admin namespace, but leave everything (for example, the root page) outside.
But in your routing example, you put everything inside the admin namespace, even the root page.
So generally, you want something like:
Rails.application.routes.draw do
namespace :admin do
# put admin stuff here
end
# put everything NOT in the admin interface outside your namespace
# you want a root route here. That's the page that'll be displayed by default
root to :your_root_stuff
# and if you have users who aren't admins, devise and authenticated routes too
# ... other stuff
end
I would like to make the homepage for an app (ie landing page) display a registration page. Unless the user is logged in - in which case they just find the "statuses" page.
Here are what I believe are the two relevant excerpts:
as :user do
get '/register', to: 'devise/registrations#new', as: :register
get '/login', to: 'devise/sessions#new', as: :login
get '/logout', to: 'devise/sessions#destroy', as: :logout
end
[...]
resources :statuses
get 'feed', to: 'statuses#index', as: :feed
root to: 'statuses#index'
Basically, I'm trying to get these two pages to swap their routes and route names. Unless, as mentioned, if someone is already signed in, then the landing page is the statuses page.
I only know a bit about this sort of thing, such as "get," and the URL aspects. If anyone could provide guidance, I'd be much obliged.
Here's the routes.rb file:
Treebook::Application.routes.draw do
resources :activities, only: [:index]
as :user do
get '/register', to: 'devise/registrations#new', as: :register
get '/login', to: 'devise/sessions#new', as: :login
get '/logout', to: 'devise/sessions#destroy', as: :logout
end
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
delete "/logout" => 'devise/sessions#destroy', as: :destroy_user_session
end
resources :user_friendships do
member do
put :accept
put :block
end
end
resources :statuses
get 'feed', to: 'statuses#index', as: :feed
root to: 'statuses#index'
scope ":profile_name" do
resources :albums do
resources :pictures
end
end
get '/:id', to: 'profiles#show', as: 'profile'
end
Why not just send the user to the statuses page, and redirect all users who are not signed in to the sign in?
class StatusesController
before_action :authorize!, only: :index
def index
#...
end
def authorize! # I believe this method is provided by devise
unless signed_in?
redirect_to new_user_session_path
end
end
end
# config/routes.rb
root 'statuses#index'
I'm having trouble having the root url route to a signed in user's profile page.
This is my config file-
get "profiles/show"
resources :players
devise_for :users
devise_scope :user do
get 'register', to: "devise/registrations#new", as: :register
get 'login', to: "devise/sessions#new", as: :login
get 'logout', to: "devise/sessions#destroy", as: :logout
end
resources :logistics
resources :notes
root :to => 'notes#index'
get '/:id', to: 'profiles#show'
I want that 'root to' to show the 'profiles#show' view. Just as the get '/:id' route does right below it.
I've tried and researched what I thought would work to no avail. Thanks for your help on this.
Hey try something like
match "/:id" => "profiles#show"