将rails应用程序部署到非root上下文 - restful_auth无效

dav*_*000 5 apache ruby-on-rails passenger ruby-on-rails-plugins

我在http://www.naildrivin5.com/scalatour上运行一个rails应用程序.它工作正常.当我使用登录应用程序时restful_authentication,我会被带到http://www.naildrivin5.com而不是应用程序.奇怪的.

这似乎是我错误配置了一些东西.此外,还有一些地方我正在手工创建一些网址,我需要访问"应用程序上下文根"(即scalatour在我的情况下)才能正确地形成URL.我最终把它扔进了我的配置,但这似乎是错的.

带有Passenger的Apache运行Rails应用程序:

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>
Run Code Online (Sandbox Code Playgroud)

passenger.conf:

LoadModule passenger_module /usr/share/passenger/ext/apache2/mod_passenger.so 
PassengerRoot /usr/share/passenger 
PassengerRuby /usr/bin/ruby 
PassengerLogLevel 2 
Run Code Online (Sandbox Code Playgroud)

/somewhere/naildrivin5.com/html/scalatour符号链接到我的Rails应用程序的public文件夹.

该应用程序工作正常,除了身份验证,使用restful_authentication.登录后,我被带到了Web服务器根目录,而不是应用程序根目录.

我应该如何/可以配置它,如何在运行时访问它,以及如何最好地配置我的本地开发环境来执行此操作(或不关心它)?

fju*_*uan 4

简答

  • 设置relative_url_rootenvironments/production.rb
  • 创建一个名为“home”的路由到我的主页
  • 修改脚手架restful_authentication代码以重定向到 home 而不是/

细节

如果您因 suburi 配置而遇到问题,则应在environments/products.rb 文件中配置relative_url_root :

config.action_controller.relative_url_root = "/scalatour"
Run Code Online (Sandbox Code Playgroud)

在我看来,您在这里描述的问题是控制器/路由问题,下一个可能的解决方案可能是:

1)我会配置这些路由:

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
Run Code Online (Sandbox Code Playgroud)

2)然后 UserSessions 控制器将如下所示:

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
Run Code Online (Sandbox Code Playgroud)

3)为了完整起见,方法“require_user”和“require_no_user”可以放置在ApplicationController中,并且将是:

  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
Run Code Online (Sandbox Code Playgroud)

此致