%e # rather than %d
Time.now.strftime("%e")
Posts Tagged ‘ruby’
Ruby Strftime Day Without the Leading Zero
Category ruby
Hello Rack
What is Rack?
Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.
- Rack API Docs
Create and name your file config.ru If rack isn’t installed get it with this command
gem install rack
In your config.ru file
require 'rubygems'
require 'rack'
class HelloRack
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello Rack!"]
end
end
Rack::Handler::Mongrel.run HelloRack.new,
ort => 8888
Reading, Writing, Removing Files and Directories in Ruby
Category ruby
These aren’t all of them, but I think they are some of the most useful.
# making a directory in another directory that doesn't yet exist...
FileUtils.mkdir_p '/path/to/your/directory/that/doesnt/exist/yet'
# recursively remove a directory and the contents
FileUtils.rm_rf("/path/to/directory/you/want/to/delete")
# write a file from the contents of another file...
File.open("/path/to/the/file.ext", "wb") {|f| f.write(@your_other_file.read)}
Send Mail in Ruby with a Pony
Category Programming
Great little gem that let’s you quickly and easily send out mail from your ruby scripts.
gem install pony require 'rubygems' require 'pony' Pony.mail :from=>"me@example.com", :to=>"you@example.com", :subject=>"hello", :body=>"world"
Installing Monk on Ubuntu with Ruby Gems
Category Monk
Installing monk like this will fail
gem install monk
You’ll need to install the wycats-thor gem first with this command
gem install wycats-thor -s http://gems.github.com
Deploy Sintra App on Ubuntu Using Apache2 and Phusion Passenger Module
Category Sinatra
Check it out http://sinatra.seanbehan.com/
This assumes Apache2 and the Phusion Passenger module have already been installed. If not you can get up to speed w/ this resource http://seanbehan.com/ruby-on-rails/new-ubuntu-slice-apache-mysql-php-ruby-on-rails-git-and/
First you need Sinatra, so install the gem
gem install sinatra
We need a home for Frank, so create the minimum number of directories in our web directory. The public directory is where we’ll server images, stylesheets, javascript etc. The tmp directory will be where we control Passenger.
cd /var/www/sinatra mkdir myapp mkdir myapp/tmp mkdir myapp/public cd myapp vim index.rb #in index.rb get '/' do "Fly me to the moon..." end
Next we need a configuration file for Rack, important the file extension is “.ru” not .rb!
vim config.ru require 'rubygems' require 'sinatra' Sinatra::Application.default_options.merge!( :run => false, :env => ENV['RACK_ENV'] ) require 'index' run Sinatra.application
Set up the virtual host
ServerName www.myapp.com DocumentRoot /var/www/sinatra/public
Now you can restart the app if you make adjustments!
touch tmp/restart.txt
Keep on singing
Inspired by http://blog.zerosum.org/2008/7/4/passenger-3-sinatra
Using Your Partials in Your Liquid Templates
Category Ruby on Rails
I’m working on a project that requires users/designers be allowed to edit the layout of their site. I’m using Liquid, Ruby templating system developed by the folks at shopify.com.
Doing a little searching I found this great post http://giantrobots.thoughtbot.com/2008/10/3/custom-tags-in-liquid The liquid documentation itself needs a little more work. But you can check it out here if you want http://wiki.github.com/tobi/liquid/liquid-for-programmers.
I worked through the example at thoughtbot blog and was having trouble with the final step, rendering the partial to the screen. The solution was really a simple step I wasn’t thinking about. Nonetheless, the information for some reason was kept out of the post and burried in the comments.
<%= Liquid::Template.parse(template).render({}, :registers=>{:controller => controller} %>
Rails: Expiring a cached page with namespaces and sweepers
Category Ruby on Rails
I’ve got some pages that are cached using their permalinks on the filesystem, such as http://example.com/about-us.html which will need to map to RAILS_ROOT/public/about-us.html … The issue I have is that I use a namespace for the admin area and the controllers in the namespace are responsible for expiring the cached pages, i.e., when the resources are updated by an admin.
Check out Rails Envy for a great tutorial for getting page caching set up: http://www.railsenvy.com/2007/2/28/rails-caching-tutorial
So what I want to do is expire the pages from inside my namespace. To accomplish this I need to use the pages route in my sweeper class.
class PageSweeper < ActionController::Caching::Sweeper
#... after_save, after_destroy... we'll exprire the cache
def expire_cache_for(record)
#permalink rather than record id like /213.html
expire_page(pages_path(record.permalink))
end
end
Since I'm using a permalink to cache the page, I need to expire it with the permalink too.
Custom Date Formats for Your Rails Application
Category Ruby on Rails
If you use a consistent date format often in your Rails applciation, it is worth it to add the format to your application environment. You can do this by adding it to the bottom of the config/environment.rb file.
Time::DATE_FORMATS[:my_custom_format] = "%A %B %d, %Y"
Now you can use it in your views like this
@post.created_at.to_s(:my_custom_format)
Which will output something like Monday May 5, 2009