← back to the blog
fousa tag
Current tag: tag
2010
My Rails templates
Ruby on Rails has a nice feature where you can generate a new rails project with a template. My templates are based on the newest version of Rails at the moment: Rails 3.0.0beta At the moment my templates handle: jQuery integration git git push to Github HAML integration HTTP basic authentication single password authentication ...
Enumerables in Ruby 1.9
CYCLE You can continue to iterate through an array. array = [1, 2, 3] array.cycle { |element| ... } This code snippet continues to iterate through the array. EACH_WITH_OBJECT The each_with_object method is almost the same as the inject method. The only thing that is different is that you don't have to return the inject...
Other stuff in Ruby 1.9
TAB When you want to output each value of an array while returning the same array. prices = [1, 2, 3] prices.tab { |price| puts price } This is not that special, but you could do some chaining with other methods. prices.tab { |price| puts price }.select { ... } Could come in handy when debugging your application. INSTA...
Enumerators in Ruby 1.9
You can convert an array into an enumerator like this: array = [1, 2, 3] enumerator = array.to_enum Then you can iterate through it like this: enumerator.next (returns 1) enumerator.next (returns 2) enumerator.rewind (resets the iteration) enumerator.next (returns 1) You can also iterate an enumerator and receive an index...
Encoding in Ruby 1.9
You can get the encoding of a string like this: "jelle".encoding You can also encode a string. "jelle".encode("US-ASCII") When you want to set the default encoding (language) used in IRB, then you should set the LANG variable before running IRB. set LANG=en_US-ASCII Now your default enc...
Symbols in Ruby 1.9
I just noticed thanks to Atog that these functionalities were already included in Ruby 1.8.7. REGEXP You can now match a symbol to a regular expression! The .match method now works with symbols! SHORTENING WITH SYMBOLS When you want each string in the array to become upcased, then you can do it this way: words = [ "test ...
Strings in Ruby 1.9
When you have the following string: string = "jelle" You can get the character on position 4 like this: string[4] In Ruby 1.8 this method would return a number. NO MORE ENUMERABLE Strings are no more enumerable in Ruby 1.9, so methods like .each aren't possible any more. But in stead you have the follo...
Blocks in Ruby 1.9
BLOCK VARIABLES In Ruby 1.9 it is now possible to add default assignments in block parameters. It's the same as in methods. lambda { |a, b = 2| puts a, b } INSTANCE VARIABLES In Ruby 1.8 it was possible to use an instance variable as a block parameter and reuse it after the block. A bit like this: [1, 2, 3].each do |@number|...
Hashes and array definitions in Ruby 1.9
HASH The most important change is the hash definition, or how to initialize a filled hash. In 1.8 it was like this: hash = { :a => "test 1", :b => "test 2" } And in 1.9 it changed a bit and became like this: hash = { a: "test 1", b: "test 2" } The old way is still available ...
Usage of lambda in scopes // 2 comments
I was using scopes in Rails 3 to show my active blog posts. Lambda was used to make sure the Time is evaluated correctly when executing the scope. scope :active, lambda { where("created_at <= ?", Time.zone.now).sorted } scope :blog, active.limit(10) This works fine until I decided to add a blog post. The last blog ...
Heroku db:push problem // 1 comment
When migrating my application to Rails 3 I ran into some small issues concerning my database. A normal db:migrate gave me a hard time so I dumped my remote database locally and corrected the errors. But then I ran into some more issues... PROBLEM I wanted to push my local database to the Heroku DB system but I ran into some small problems....
Heroku on Rails presentation on Barcamp
Yesterday I did a presentation on Heroku on my very first Barcamp Antwerp. And I like it a lot! So if you're interested in seeing my presentation, here it is! You can find my presentation on SlideShare, or embedded below. HerokuView more presentations from Jelle Vandebeeck. Have fun playing with Heroku!
Overwrite custom field errors in Rails3
When using form helpers in your form to display your textfield you always have to match your CSS style to match with the div's that are added by Ruby on rails when an error occurs. This is the form helper: f.text_field :title This is the generated tag with the error div surrounding it: <div class="fieldWithErrors"&...
Sanitize order clause in ActiveRecord query
After some testing we came (me and JB) to the conclusion that it was not possible to inject hazardous SQL into the order statement. But if you don't want any injection at all you can still use this. I'm trying to dig a little deeper into this! This tutorial concerns Rails 2.x, I'll have to check whether I have to do this for Rails 3.x! I d...
Staging environment on Heroku
Sometimes you want to test your application on one of your production servers so you can let your clients test your application in real life! What I wanted to do is point the master branch to dev.testapp.com and the production branch to testapp.com. The subdomain dev.testapp.com is my staging environment where the clients can play with the ap...
Contact form validations with AR helpers
This is how you validate a contact form with the validation helpers used in ActiveRecord. This piece of code enables you to validate: presence of the name and content format of the website (example: http://www.fousa.be) format of the email address (example: test@fousa.be) value of the snow field (snow must be "cold") I also imple...
My Git integration with Terminal
The following scripts are added to my ~/.bash_profile for some Git integration. BRANCH AUTOCOMPLETION This piece of code autocompletes the branches when you press TAB. So "git checkout ma" and press TAB, will result in "git checkout master". _complete_git() { if [ -d .git ]; then branches=`git branch -a | cut -c 3-` tags=`git ta...
Nasty load problem with Rails 3
The following line of code loads the data from config/config.yml file into a global CONFIG variable. The piece of code below is executed by the initializer initializers/load_config.rb. CONFIG = File.open(RAILS_ROOT + "/config/config.yml") { |file| YAML::load(file) } This works great for Rails 2.x.x. But in Rails3 it fails... ...
Heroku and Github for the same project
WHAT'S THE PROBLEM? I have a local branch called master. And I'm currently pushing this branch to my remote Github branch. Now I also want this branch to be used as my Heroku master branch. So I can deploy my code to the Heroku servers. What is my problem? I have a config.yml file with sensitive information inside. And I want it to be push...
Generate a previous rails project // 2 comments
Because I'm a close follower I already installed Ruby on Rails 3.0.0 beta... But now every time I want to generate a rails application, it's generated with the newest beta version. Below is a small code snippet on how to generate a rails project with a previous version of Ruby on Rails. RUBY ON RAILS 1.2.3 This is how you generate a Rai...
Convert MySQL DB to PostgreSQL DB
When I was converting my blog to the heroku, I decided to convert my local database from MySQL to PostgreSQL. This for developing purposes because it's a bad practice to have 2 different systems in development and production. This is my current setup: 1 local MySQL database with my production dump in it. A new PostgreSQL database that is up a...
Create a user for your MySQL database
When you want to start to develop a new web application then you'll have to create a new database. In my case I prefer using a MySQL database because it is the same as on my remote server. And I also prefer to create a specific user for each database. And here is how you do that! CREATE USER Go to your console and type the following line t...
Clean URL's in Rails // 1 comment
Another great and awesome informational article by me! PROBLEM When you create a new model (for example Post) you'll also have a posts_controller. This controller will be called from following URL: /posts/. But because I like clean URL's, I prefer it to be called /blog/. SOLUTION What I certainly don't want to do is change the nam...
Reinitialize your Git repo // 1 comment
This came in very handy when you want to reinitialize your Git repository from scratch without losing the current folder structure. WHY? I started developing an iPhone application and for API testing purposes I manually added my username and password in the code. And by accident I committed the change... So now my password was in the Gi...
Using multiple Ruby versions
I was working on a project that still on Ruby 1.8.5 and locally I was using 1.8.7. At first you'd think, no problem! But reality showed me I was wrong... The count method wasn't supported on Arrays in the older version but I used it in my code and it worked with the newer version... So this failed in production! That's why I looked for a w...
Introduction on Cappuccino
A few weeks ago I decided to dive into the basics of Cappuccino. A nice web based javascript framework that gives the user the look-n-feel of a real (Mac OS X) desktop application. It's not only different for the user but also for the developer... He now can create web applications with Objective-C like code! So for those who adore developing...
2009
JRuby on RuPy
This is an article on the JRuby presentation from the Ruby/Python conference in Poznan, Poland. In short, JRuby is a Ruby implementation of JVM, it's native-threaded and pretty fast. THE COMMAND LINE In JRuby you can access the command line IRB client, but here it's called JIRB. It runs on the JVM (Java Virtual Machine) and you have access...
RuPy, a Ruby/Python Conference (day 2)
And we're back attending RuPy on the second day. For those of you who missed DAY 1, just check out the linked post. I had to split up, because I didn't want to overkill the post. I also attended the very interesting "Mastering GIT" talk, but I will make a short post on it for your convenience! Let's start! RABBITMQ This is a handy li...
RuPy, a Ruby/Python Conference (day 1)
Thanks to my lovely job at Belighted I was able to attend the RuPy conference in Poznan, Poland. It was a nice conference in a somewhat awkward city. With this blogpost I will make a quick review of some of the talks! I didn't review all the talks, because some talks did not match my personal interests. So I have nothing against the other spe...
Canvas drawing
This is just an example of how I created the image below. The image is drawn using the canvas tag in HTML5. var canvas_element = document.getElementById("canvas_element"); var canvas_context = canvas_element.getContext("2d"); // Draw body path canvas_context.beginPath(); canvas_context.moveTo(50, 250); canvas_context.lineT...
Things I didn't know about validations
VALIDATE INCLUSION When you want to check if a value is included in a list of values, you can use the validation helper below. The array with the possible values is passed with the :in symbol. validates_inclusion_of :size, :in => %w(small medium large), :message => "{{value}} is not a valid size" VALIDATE EXCLUSION...
Things I didn't know about migrations // 1 comment
UPDATE EVERY EXISTING RECORD After you altered your table and added for example a column. You can tell the migration to update every record already in the altered table with a value defined by you. def self.up change_table :products do |t| t.boolean :price_available, :default => false end Product.update_all ["price_available ...
Merb, a fast Ruby framework
Merb is a lightweight Ruby framework that has basically the same functionality as Ruby on Rails, but is much faster! The big difference is that Merb has a small core, javascript, template engines and database connections are plugged in and not added to the basic core. WHY MERB? Merb is thread safe. Merb can handle multiple file uplo...
Haml, another templating engine // 2 comments
Haml (HTML Abstraction Markup Language) is another templating engine for Ruby on Rails/Merb. Before I used erb, but after a while my template started to look very messy, and I wanted to clean it up. Haml is a great way to do this! This is how you install it on your system: > sudo gem install haml Next you have to activate the haml p...
Sinatra, rapid web development // 1 comment
Sinatra is a great way to use ruby for rapid web development! When you just need to create a simple website for a small company of just for a friend. Below is a small example on how to use it! A SIMPLE WEBAPP Installing sinatra on your server is very easy, just type in the following: > sudo gem install sinatra After that you h...
Thin, a fast and simple web server // 1 comment
For some strange reason my mongrel server doesn't seem to be running on Snow Leopard. When I start my rails server with the following command it takes forever... > ./script/server Using webrick resulted in the same slow startup, but less slow then mongrel tough! Don't know, don't care what Snow Leopard did! But I need to get to t...
Problem with SQLite3 after Snow Leopard upgrade // 1 comment
When I first upgraded to Snow Leopard everything seemed to be working just fine. But then I tried to migrate my sqlite3 database for my rails web-application... The error I received was the following: rake aborted! uninitialized constant SQLite3::Driver::Native::Driver::API That was the end of my development until I found the solution...
Twitter oauth authentication in Rails // 3 comments
These days everyone is on Twitter! So building a social networking webapp without Twitter integration would be kind of stupid. That is why I started looking around how to do this in a decent - oauth - way. After googling around I stumbled upon this gem: moomerman-twitter_oauth gem. But I had some troubles implementing it, therefor this article! ...
Using CSV in Rails
I'm back with a new article. This time it's a very simple tutorial on how to read data from a comma separated file (.csv). This can be very handy when you try to do a batch import into the database. I use this to upload multiple words at once on jaaper.be. First thing to do is import the csv libraries. You can do this as easy as I do it below...
Uploading a rails plugin to Github // 1 comment
I recently looked for a nice SVN repository to store my rails plugins for public use. But I searched the web and found that the most common way was to put the rails code on a github repository. So I created my github account, and then an open source repostiory (named 'plugin-test'). Before you continue with this tutorial, you should check thi...
Fix the xml-mapping gem in rails 2.x
In a previous article I explained how to use the xml-mapping gem to map a XML tree to your models. But what I didn’t mention is that a exception is raised when using this gem in rails 2.x. When you try to save the model to an XML file it goes wrong. In rails 2 some methods became deprecated and therefore I created a plug-in that patches the gem....
Using the rbVimeo gem on a shared hosting
For my blog I wanted to display my last 3 movies I posted on the Vimeo website. For this I used the rbVimeo gem I found on the web. But the only problem was that I couldn’t install it on my server. I’m currently hosting my websites on a shared webserver at Joyent. So I tried to find a way to get around this problem. And it turned out to be fairl...
2008
Model based XML in rails // 2 comments
On my blog I had to find a way to generate the content of the services page. This page includes date from different API’s like twitter, flickr, and so on... But the problem was that the load was to high, and the loading time was too long. Therefore I decided to get the data from an XML file. This XML file was generated by a batch rake task (t...
Integrating gravatars in your rails app
For this blog I wanted to integrate gravatars in my comment system. To do this I searched for a nice plug-in that generated the img tag for me. And I found a great one. One that is easy to install and use. To install the plug-in run the following commands from the command line. > cd ~/rails_directory/ > ./script/plugin install svn...
Integrating acts_as_taggable_on_steroids in your rails app // 6 comments
For this blog I needed to have some kind of plug-in to integrate tags and add them to my blog posts. For this I used the acts_as_taggable_on_steroids plug-in, which is great when you need an easy way to use tags and build your own tag cloud. This tutorial will show you how to use the tags and the cloud in your rails application. You can have the...
Active_record_base_without_table for rails 2.x // 1 comment
I use the active_record_base_without_table from http://agilewebdevelopment.com/plugins/activerecord_base_without_table to validate the fields of my contact form. The reason why is because all the validation rules are at the same place and I don't like to do manual checks in the controllers. So that is why I use a model that has data, but doesn't...
Using the simplify_time_calculations plug-in // 3 comments
I'm currently busy with creating a administrative website to keep a flight log of all the flights made by a member of my soaring club. Just in case you wonder what I'm talking about, I'm a passionate glider pilot at http://www.dewouw.be. And we always have a start time (format HH:MM) and a landing time in the database. But because it was to hard...