4

Haven't been able to find anything specific to this issue (other searches deal with forms and such). It's probably a simple oversight on my part. But what on earth am I missing?

GOAL: I'm simply trying to redirect from the /login page URL to the /dashboard URL if a session exists.

EXPECTED OUTCOME: Calling redirect_to dashboard_index_url or redirect_to '/dashboard' should go to https://mydomain/dashboard

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

ATTEMPTED SOLUTIONS:

  • Manually type the URL https://mydomain/dashboard after creating a session, RESULT: works, so the proper route seems to exist
  • Make manual route in routes.rb, RESULT: behavior is exactly the same as resource routing with the missing slash
  • Clear browser cache, use different browswers RESULT: all exhibit same behavior

Here's what I have (abbreviated to 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 the 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

Richard Peck
  • 76,116
  • 9
  • 93
  • 147

3 Answers3

3

SOLVED:

Well, I found the problem and it was not actually Rails, it was Apache-Passenger. There was a config with a redirect (so that all HTTP get redirected to HTTPS) at the apache level that didn't have the trailing slash floating around and causing trouble. (smacks forehead)

Gotta love those red herrings. Thanks so much guys for the quick help!

1

for use dashboard_index_url, you have to write it in your routes file. However as you've created in routes.rb a dashboard resource, a dashboards_url is available to you and it leads to /dashboards/index

Other way to nail this task is create a mapping

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

and dashboard_index_url and dashboard_index_path will be available for you

Update 1

Please, look on this SO question the problem is that path give you relative route and url - absolute. that's why you don't have additional slash in your path. Try path instead of url and it should work.

Update 2

try dashboard_index_path

Community
  • 1
  • 1
Tetiana Chupryna
  • 1,044
  • 1
  • 11
  • 28
  • For some reasone, even if I do that or use `redirect_to '/dashboard/index'` directly and not the variable it still strips that leading space so that I get `https://mydomaindashboard/index`, argh – musicman3569 Jan 19 '16 at 19:49
  • Great suggestion and thanks for the quick responses. Unfortunately using `redirect_to dashboard_index_path` goes to `https://mydomaindashboard` with no slash as well. Yet manually adding the slash and reloading gives me the proper page. I'm truly stumped. – musicman3569 Jan 19 '16 at 20:04
  • it was Apache that was the problem, I posted the solution below. Thanks again! – musicman3569 Jan 19 '16 at 20:22
0

How about changing your routes to

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

You can even tailor the resulting link to something more specific to your user by

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

On a side-note, you should try cleaning up your routes as you go. There are a lot there that you are not using. Change it to resources :dashboard, only: [:index] and add to it as you need. E.g only: [:index, :show] etc. If another developer was needed to modify your app one day we'd look at your routes and perhaps make incorrect assumptions about what we can do.

Justin

RuNpiXelruN
  • 1,850
  • 2
  • 17
  • 23