开发者

Simplest way to add Ruby script to a static website for AJAX

开发者 https://www.devze.com 2023-03-19 02:42 出处:网络
I have a simple portfolio website, which is just a set of static HTML files and some jQuery. Right now I\'m trying to add a simple contact form which is submitted via AJAX, so I need to somehow proc

I have a simple portfolio website, which is just a set of static HTML files and some jQuery.

Right now I'm trying to add a simple contact form which is submitted via AJAX, so I need to somehow process the request on the server side.

The whole thing is running on a VPS with many other Rails applications on nginx. If I had PHP installed, it would've been easy task of just adding one simple PH开发者_如何学GoP file and call it directly via AJAX, but I don't really know how to do that in Ruby.

I basically have

server {
    listen 80;
    server_name example.com;
    root /var/apps/example/com;
}

for the static website, and now I need to somehow say, that file contact.rb should be executed as a Ruby file.

I thought that maybe using Sinatra for this would be a good idea, but I don't know how to bind/route it to a specific location on the domain. For example, the form might submit the AJAX request to example.com/contact, which would then just send out the email.

The problem is, that all of the other Rails applications, where I could process the request are running on a different domain, so I can't use AJAX call directly to some other app and I need to create a separate script on this domain.

I know I could rewrite the whole static website to be running on Sinatra, which would eliminate this issue, but I'd rather prefer doing this with some simple approach, as it's all just for one simple contact form.


Another option is to use a contact form handling service like EmailMeForm. There seems to be several similar systems available. Granted it's not as "cool" as rolling your own system. But it could work for your static website purposes.

http://www.emailmeform.com/


in apache httpd.conf :

AddHandler cgi-script .rb
Options ExecCGI

#!/usr/bin/env ruby
# in test.rb
require 'cgi'
puts "Content-Type: text/html \n\n" #wichtig

params = CGI.new
query = params['q'] #simple as that!

$stderr.puts "Remote Addr: #{params.remote_addr}"


Use sinatra and learn about defining routes. It is exactly what you are looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号