Rails redirect_to creates invalid url

Could not find anything specific for this problem (other search queries relate to forms, etc.). It was probably a simple oversight on my part. But what am I not seeing?

PURPOSE: I am simply trying to redirect a URL /loginto a URL /dashboardif a session exists.

EXPECTED RESULT: Challenge redirect_to dashboard_index_urlor redirect_to '/dashboard'should go tohttps://mydomain/dashboard

CURRENT RESULT: if I go to https://mydomainafter creating a session, it redirects me to https://mydomaindashboard, note the missing slash

FOLLOWING DECISIONS:

  • Manually enter the URL https: // mydomain / dashboard after creating the session RESULT: so the correct route seems to exist
  • Make a manual route routes.rb, RESULT: The behavior is the same as the resourcerouting of a missing slash
  • Clear browser cache, use different browswers RESULT: all demonstrate the same behavior

Here is what I have (abbreviated to the relevant parts):

class LoginController < ApplicationController
  def index
    redirect_to dashboard_index_url if session[:user_id]
  end
  #...
end

class DashboardController < ApplicationController
  before_action :require_login # calls redirect_to root_url unless session[:user_id]

  def index
    #...
  end
end

# In routes.rb:
resources :login
resources :dashboard
# have also tried things like (removed the above line for these)
get 'dashboard' => "dashboard#index"

@Ryan Here is the current output for routes:

$ rake routes Prefix Verb URI Pattern Controller#Action login_index GET /login(.:format) login#index POST /login(.:format) login#create new_login GET /login/new(.:format) login#new edit_login GET /login/:id/edit(.:format) login#edit login GET /login/:id(.:format) login#show PATCH /login/:id(.:format) login#update PUT /login/:id(.:format) login#update DELETE /login/:id(.:format) login#destroy dashboard GET /dashboard(.:format) dashboard#index dashboard_index GET /dashboard(.:format) dashboard#index POST /dashboard(.:format) dashboard#create new_dashboard GET /dashboard/new(.:format) dashboard#new edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit GET /dashboard/:id(.:format) dashboard#show PATCH /dashboard/:id(.:format) dashboard#update PUT /dashboard/:id(.:format) dashboard#update DELETE /dashboard/:id(.:format) dashboard#destroy root GET / login#index

+4
source share
3 answers

DECIDE:

, , Rails, Apache-Passenger. ( HTTP HTTPS) apache, , . ( )

. !

+2

dashboard_index_url, . , routes.rb , dashboards_url, /dashboards/index

-

get 'dashboard' => "dashboard#index" as: :dashboard_index

dashboard_index_url dashboard_index_path

1

, SO > , url - . . url, .

2

dashboard_index_path

+1

get '/login', to: redirect('/dashboard')

-

get '/login', to: redirect('/dashboard'), as: '/dashboard/user[:username]', .

, . , . resources :dashboard, only: [:index] , . , only: [:index, :show] .. , , , , .

0

Source: https://habr.com/ru/post/1625019/


All Articles