2014-11-04 59 views
0

这里可能是一个新手问题。我试图通过Passenger和Apache部署一个Sinatra应用程序。当部署在VirtualHost根目录时,该应用程序完美工作,但在尝试将其部署到子URI时,无法正确处理表单帖子。如何处理部署到子URI的Sinatra应用程序中的表单?

A中的应用程序的非常简化的版本如下:

测试app.rb:

require 'sinatra/base' 
require 'haml' 

class TestApp < Sinatra::Base 

    get '/' do 
    haml :ask 
    end 

    post '/submit' do 
    if params[:test_string].nil? || params[:test_string].empty? 
     redirect '/' 
    end 

    @test_string = params[:test_string] 
    haml :result 
    end 
end 

layout.haml:

!!! 
%html 
    %head 
    %title Test App 
    %body 
    = yield 

ask.haml:

%form{:action => '/submit', :method => 'post'} 
    %legend 
    Get a string 
    %p 
    %label{:for => ''} Please enter a string: 
    %input{:type => 'textbox', :name => 'test_string', :id => 'test_string'} 
    %p 
    %input{:type => 'submit', :value => 'Submit >>>'} 

result.haml:

%p== Here's your string: #{ @test_string } 

什么似乎正在发生的是,表单POST不会正确的URI - 它似乎忽略子URI配置,并且要到虚拟主机的根在那里,当然,还有没有代码来处理路线。我已经检查并重新检查了Apache配置,这似乎不是问题所在。

<VirtualHost *:80> 
    ServerName my.domain.com 
    DocumentRoot /var/websites/home 
    <Directory /var/websites/home> 
    Allow from all 
    Options -MultiViews 
    </Directory> 

    Alias /test-app /var/websites/test-app/public 
    <Location /test-app> 
    PassengerBaseURI /test-app 
    PassengerAppRoot /var/websites/test-app 
    </Location> 
    <Directory /var/websites/test-app/public> 
    Allow from all 
    Options -MultiViews 
    </Directory> 
</VirtualHost> 

有什么办法(比硬编码的形式等),以确保表单发送到子URI,在那里我的应用程序可以处理的,而不是发布到虚拟主机的根呢,?

回答

0

不知道这是处理这一目标的最佳方式,但我工作围绕这一问题通过使用Sinatra的URL帮手ask.haml:

%form{:action => "#{ url('/submit') }", :method => 'post'} 
    %legend 
    Get a string 
    %p 
    %label{:for => ''} Please enter a string: 
    %input{:type => 'textbox', :name => 'test_string', :id => 'test_string'} 
    %p 
    %input{:type => 'submit', :value => 'Submit >>>'} 

通过这样做,我的形式发布到应用程序的子URI而不是VirtualHost根。

相关问题