Is there a PHPish way of doing webpages with Ruby?

前端 未结 4 1667
予麋鹿
予麋鹿 2020-12-17 04:56

Is there a framework or something out there so that I can develop webpages in Ruby the same way I can as PHP. Something like



        
相关标签:
4条回答
  • 2020-12-17 05:14

    The default behavior for PHP is to run as CGI scripts, which means that the web server calls php-cgi <path/to/php-script> or something similar, passing quite a lot of environment variables. To do the same with Ruby, you need to setup a script to handle .rb files. This varies wildly depending on your web server, but if you are using Apache 2.2, put this in your httpd.conf or .htaccess file:

    Action ruby-cgi /path/to/ruby-cgi
    AddHandler ruby-cgi .rb
    # You might want to add this too:
    DirectoryIndex index.rb index.html
    

    You could either specify the path to your ruby executable (run which ruby to get the path), or to any other script that accepts a filename as the first parameter. If you use the ruby executable, nothing magical happens, and you can't insert erb into the file without adding some ERB compiling yourself. However, you could use my ruby-cgi script, which does several things:

    • First, it takes the file and interprets it as ERB, this make the syntax look more like PHP (see below for an example).
    • Second, it initializes the CGI object into the global variable $CGI. See below for an example on how to use this.

    This is a simple example script on how you can use the ruby-cgi "magic":

    <% header "Content-Type" => "text/html" %>
    <html>
      <head>
        <title><%= $CGI['title'] %>
      </head>
      <body>
        <h1><%= $CGI['title'] %>
      </body>
    </html>
    

    Let's say you put this into the webroot with the name example.rb. If you then access this with a URL similar to http://example.com/example.rb?title=Hello%20world it should set the title to "Hello world", and it should display a <h1> with "Hello world" in it.

    If you find any bugs with the script, feel free to fork the gist and update it.

    0 讨论(0)
  • 2020-12-17 05:14

    Have you seen Nanoc? It is very simple ruby compiler that outputs static pages through a template engine. May be too simple for your needs but it is handy some times.

    0 讨论(0)
  • 2020-12-17 05:15

    Two words: Sinatra and ERB

    (For lightweight sites, at least).

    Sinatra is a simple HTTP server, ERB is a templating system that acts similar to templating in PHP.

    0 讨论(0)
  • 2020-12-17 05:36

    You can, with https://github.com/migrs/rack-server-pages

    Instruction at that page.

    Summarizing:

    • gem install rack-server-pages

    • Create config.ru in the root folder of your "application":

      require 'rack-server-pages' run Rack::ServerPages

    • Create public/index.erb:

      <h1>Hello rack!</h1> <p><%= Time.now %></p>

    • execute rackup, your Ruby powered pages are ready to be served.

    • It also works with other application servers, for example, for passenger standalone, you just need to go to that folder and run passenger start.

    0 讨论(0)
提交回复
热议问题