views:

255

answers:

1

I'm running a rails app on http://www.naildrivin5.com/scalatour. It works fine. When I log in to the app using restful_authentication, I get taken to the http://www.naildrivin5.com instead of the app. Weird.

This seems like I've misconfigured something. Further, there's a few places where I'm hand-creating some urls, and I need access to the "application context root" (i.e. scalatour in my case) to form the URL properly. I ended up throwing it in my configuration, but this just seems wrong.

Apache, with Passenger, running a Rails app:

Apache conf:

<VirtualHost 69.89.3.135:80> 
  DocumentRoot /somewhere/naildrivin5.com/html
  ServerName naildrivin5.com 
  RailsBaseURI /scalatour  
  PassengerPoolIdleTime 5 
  # other things not related
</VirtualHost>

passenger.conf:

LoadModule passenger_module /usr/share/passenger/ext/apache2/mod_passenger.so 
PassengerRoot /usr/share/passenger 
PassengerRuby /usr/bin/ruby 
PassengerLogLevel 2 

/somewhere/naildrivin5.com/html/scalatour is symlinked to my Rails app's public folder.

The app works fine, except for authentication, using restful_authentication. After I log in, I'm taken to the web server root, not the application root.

How should/can I configure this, how can I access this at runtime, and how can I best configure my local dev environment to do this (or not care about it)?

+1  A: 

Hello

Short Answer

  • setting relative_url_root in environments/production.rb
  • creating a route called "home" to my home page
  • Modifying the scaffolded restful_authentication code to redirect to home instead of /

Details

If you're having troubles because of the suburi configuration, you should configure the relative_url_root in your environments/production.rb file:

config.action_controller.relative_url_root = "/scalatour"

In my opinion, the problem your describing here is a controller/routes issue and a possible solution may be the next:

1) I would configure these routes:

ActionController::Routing::Routes.draw do |map|
  [...]
  map.home 'home', :controller => 'welcome', :action => 'show'

  map.login "login", :controller => "user_sessions", :action => "new"

  map.logout "logout", :controller => "user_sessions", :action => "destroy"

  map.root :login
end

2) Then the UserSessions controller would look like:

class UserSessionsController < ApplicationController
  before_filter :require_no_user, :only => [:new, :create]
  before_filter :require_user, :only => :destroy

  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      redirect_back_or_default home_path
    else
      flash[:error] = "Sorry, unrecognized username or password."
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    redirect_back_or_default login_url
    reset_session()
  end
  end

3) Just for completeness, the methods "require_user" and "require_no_user" could be placed at the ApplicationController and would be:

  def require_user
    unless current_user
      flash[:error] = t("You must be logged-in to access this page")
      redirect_to login_path
      return false
    end
  end

  def require_no_user
    if current_user
      redirect_to home_path
      return false
    end
  end

Best regards

fjuan
This was it; I'm going to edit the post to highlight exactly what needed doing. Thanks!
davetron5000
I'm glad that helps you. Thank you for summarizing it!!
fjuan