Is it possible to run single .erb file without rails in apache? - ruby

I got a ubuntu machine and im tryying to create a simple view with ruby.Im well familiar with rails but i wanted to a simple standalone(if possible) like a php one with ruby.I got apache2 installed on my machine.I also got nginx and passenger.
The file i want to create is a simple 3+2= <%= 3+2 %>.Any clues?

Look into using sinatra for simpler implementations. It runs as a webserver as well but is much more lightweight than rails, and has erb support. Remember, erb is a library and not a native ruby feature, so you need a way to compile the view to include the ruby variables.
Its also possible to write your own webserver logic using Rack, and hook into erb to render the view, but thats probably more work than you want to do. With sinatra, rendering an erb is as simple as:
require 'sinatra'
get '/' do
erb :index
end
(If you had a file in a views/ directory named index.erb)

Ruby comes with the erb executable.
You can compile the file to HTML with the following shell command
erb input.html.erb > output.html

Related

Has anyone gotten Padrino to work with Sprockets and Compass?

I'm on Padrino 0.10.7 and I haven't been able to get Sprockets to recognize Compass in the load path.
This is the only question on SO I've found on this topic.
Got this working with the following:
padrino-pipeline gem - going down the Sprockets route
Somewhere (I used my app.rb file) you will need the following:
Sass.load_paths << "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/compass/stylesheets"
Sass.load_paths << "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/blueprint/stylesheets"
Not sure if there is a simpler way. Feels like there should be. Also, any neat ways of getting image-url() sass method working?
EDIT: There is a way to get image-url working - if you thought the above was messy, add this to your app.rb too...
assets.context_class.class_eval do
def settings
YourAppName::App.settings
end
include Padrino::Helpers::AssetTagHelpers
end
(where YourAppName is the name of the module that describes your application, at the top of app.rb)

How to turn my rails app into static content?

My rails app fetches a bunch of xml feeds once a day, loads them into the db and then displays them in aggregate. I'm thinking that I can save on server memory if I just output the pages as static files and let them be served directly by the front-end server (nginx in my case). I asked in an IRC room and was told to not use rails and create the files using rake tasks. However, I'm wondering what the easiest way would be to go about doing this. Layout, asset files and content are in different places in rails obviously, so I guess I would need to combine the layout and content and then insert the css/javascript.
Any thoughts/ideas are welcome.
[Solved]
I ended up using the examples from render_to_string from a rake task and made some tweaks to get the following code inside my rake task:
views_path = Rails.root.to_s + "/app/views"
av = ActionView::Base.new(views_path)
av.class_eval do
include ApplicationHelper
end
products = Product.all
a = av.render(:template => "products/show", :layout => "layouts/application", :locals => { :#products => products } )
This then renders both the template and the layout, and allows the use of the #products instance variable inside the template just as you would if you were using a controller.
Then I just need to write the output of the render to a file.
For a task like this you can use Rails' built in caching mechanisms.
There is another stack overflow post which shows some example code of how to build code to write that cache manually from something like a rake task.
Perhaps middleman or jekyll could be used?
I've only used middleman, but you could use a rake task and support script to get the latest xml feeds and stick that into middlemans data dir (i.e. data/feeds.yml), then use your existing layouts to render that yaml file. Middleman and rails share a lot of similar tech for rendering etc.
You'd have to modify your layouts a little bit.
You could probably find gems to replace yaml with something else if you wanted.

Tutorials to Testing using RSPEC on PADRINO framework on RUBY

I am new to Ruby and have been asked to use it in our new project. We have also been asked to use Padrino (Sinatra) as backend/framework. For testing we have been asked to use Rspec. I have been hunting for tutorials for long that would guide in using Rspec for Ruby on Padrino. What I get is mostly with reference to RoR. But, I am in need of Ruby on Padrino.
Please guide me for the same with any starters/guides/references/discussions, etc.
Please correct me, if I am wrong anywhere. May be I haven't searched with the right combination of words/phrases for my issue.
I am using Ruby 1.9.3 and Padrino v.0.10.6.
Note : I have also referred the SO question, but it didn't help.
I never used Padrino, but it seems that it isn't much different from Sinatra.
I suggest reading Sinatra and RSpec resources.
You can get started with this:
How to use rspec in a Sinatra Application
Sinatra, RSpec and DataMapper: Configuring and using a database for tests
And by reading specs that were written by other people on GitHub. These are some of mine, but they are not the cleanest thing that exists.
EDIT: a short tutorial
Getting started with this framework is much quicker and easier than with Sinatra. :)
Install Padrino: gem install padrino
Create an application: padrino g project myapp -d datamapper -t rspec
The command speaks for itself. :)
Run the tests: rspec --color
No tests were found, obviously. Let's create one in spec/hello/hello_spec.rb:
require File.dirname(__FILE__) + "/../spec_helper.rb"
describe "get '/'" do
it "should display hello world" do
get '/'
last_response.body.should == "Hello world!"
end
end
Run the tests again.
They failed, because no route get '/' exists. Let's create it.
In app/controllers/hello.rb:
Myapp.controller do
get '/' do
"Hello world!"
end
end
Run the test: it passes!
Check Padrino's documentation for more information and cool features, such as the controllers generator and the admin interface.
Good luck!

Is there a Rack or Sinatra based environment configuration utility?

Is there anything in the Sinatra / Rack world similar to Rails configuration loading scheme that loads one of the config\enviroments\*.rb files depending on Rails.env
I know I could develop one pretty easily, i was just wondering if there was something already in place.
If you're following the Rails convention of putting a file for each environment in config/environments/environment_name.rb, you can put something like this in your Sinatra app, or for Rack in your config.ru file:
Dir.glob(File.dirname(__FILE__) + "/config/environments/#{settings.environment}.rb", &method(:require))
With some minor modifications you could make it load other file locations/combinations. Sinatra's configure blocks work just as well, too.
It turns out that there is something from Sinatra, that provides a similar, though limited, functionality.
See the code:
https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1120
So that you can do this:
class MyApp < Sinatra::Base
configure :development, :test do
#only executes this code when environment is equal to one of the passed arguments
# I'm pretty sure Sinatra sets this based on ENV['RACK_ENV']
end
end
There is one called Sinatra::ConfigFile, which now lives in Sinatra::Contrib http://www.sinatrarb.com/contrib/config_file.html
There's lots of useful stuff in there.
I adapted mine from monkrb.com (it's also yaml in RoR anyways)
YAML.load_file(path_of "config/settings.yml")[RACK_ENV]
e.g.
http://github.com/codepants/yasumi/blob/master/config/settings.yml

How do I test Sinatra helpers from irb?

I am trying to call a helper method in my Sinatra application from irb in order to test its output. I know I can get a console using this tip, and I've tried racksh as well. But if I do a "defined? my_helper" I always get nil. There must be some simple way of getting at those helpers. I have a feeling that this means digging through the architecture of Rack a little bit. Any hints?
You can of course test your Sinatra helpers via IRB.
Suppose you had a modular Sinatra app with a helper method foo that printed "baz":
require "my-app.rb"
MyApp.new.helpers.foo # => "baz"
I am trying to call a helper method in my Sinatra application from irb in order to test its output
Instead of testing it over the command line, use RSpec. See How can I test helpers blocks in Sinatra, using Rspec?

Resources