Having trouble routing to profile homepage, rails - ruby-on-rails

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"

Related

Rails Devise, No route matches '/users/sign_out' -> SignOut/Logout Routes problem

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

Devise: Issue with routing using devise_scope

I'm trying to route my User model sign up form to /sign_in. Tried several things and just used the example given in the devise docs.
I copied in to my routes.rb
devise_scope :users do
get "sign_in", to: "devise/sessions#new"
end
and rake routes says it should be working and shows what I expected.
http://s1.postimg.org/do335u8v3/Screen_Shot_2014_06_18_at_11_34_34.png
What's going wrong here?
Try this instead.
devise_for :users do
get '/users/sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get '/users/sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
get "/users/sign_up", :to => "registrations#new"
end

How to change the home page of my Rails app to show Registration page instead?

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'

devise won't redirect to after_sign_up_path when using customized devise routes

I invoke the after_sign_up_path_for(resource) by defining it in an inherited registrations_controller:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
new_user_profile_path(resource)
end
end
This redirect works fine if I don't customize the routes. however, when I do, it no longer works. My routes file has:
devise_for :users, skip: [:sessions], controllers: { registrations: "registrations" }
devise_scope :user do
get 'signin' => 'devise/sessions#new', as: :new_user_session
post 'signin' => 'devise/sessions#create', as: :user_session
delete 'signout' => 'devise/sessions#destroy', as: :destroy_user_session
get 'signup' => 'devise/registrations#new', as: :new_user_registration
post 'signup' => 'devise/registrations#create', as: :user_registration
root to: 'pages#home'
end
Specifically, the post 'signup' => 'devise/registrations#create', as: :user_registration line messes things up. If that's not there, it works fine. However, if that's not there, then when registration fails due to validation error, the URL defaults to /users rather than /signup, which is an undesired behavior.
Anyone know if this is something I am doing wrong or if there's a bug in devise?
The issue with your custom routes is that you are pointed back to Devise controllers:
post 'signup' => 'devise/registrations#create', as: :user_registration
That obviously is not going to work. Point to your own controller instead:
post 'signup' => 'registrations#create', as: :user_registration
Just briefly looking at your code provided, I don't think the route you want is new_user_profile_path. You probably want user_profile_path, can you run rake routes and paste the output here?

Rewriting Rails routes

Currently I am working on a blog engine in RoR and I encounter severa issues with routing.
The routes.rb looks like this:
match '/admin', :to => 'posts#new'
match '/get/:id', :to => 'posts#get'
match '/new', :to => 'posts#new'
delete '/:id', :to => 'posts#destroy'
post '/edit/:id', :to => 'posts#update'
put '/edit/:id', :to => 'posts#update'
get '/edit/:id', :to => 'posts#new', :as => 'post'
get '/:slug', :to => 'posts#show', :as => 'post'
root :to => 'posts#index'
and I would like to transform it in something like:
resources :admin do
resources :posts
end
Any help would be very appreciated.
A bit more information is needed. What do you want to place in the admin resource? Only posting, or also editing?
But a few tips to get started:
- You have to split your posts-controller. Make a subfolder in the controllers called admin (the resource name). Move the admin-functions to this controller, and leave the public posts-function (index and show) in the normal posts_controller.
- Do the same for the views.
And, i suspect you want the routes to be:
namespace :admin
resources :posts
end
get '/:id', :to => 'posts#show'
root :to => 'posts#index'
Then you can put some form of authentication to the admin namespace.
Hope this helps you on the way.

Resources