Add the i18n-js gem to your gemfile.
Before you can really start using the localizations, you have to set the current locale. Do this in your application.html.erb file. (But it doesn’t really matter where you set it, just do it before the call to translate)
And now just translate the same way you do in your rails app.
Here is an example of my en.yml file.
Using number in your locales with pluralization.
Use a default value when no translation is present.
Use localizations for number formatting.
For more info check Github.
What’s a Natter?
Most of us here at 10to1 are avid Twitter users and being the
social media experts geeks that we are we’ve also got a Facebook account. The problem is; we like Twitter much more than we do Facebook. So when a Facebook-friend commented on something we’ve posted via Twitter, we didn’t know it until hours later.
So we felt an itch and the Rails Rumble was an excellent excuse to scratch it.
What’s a Rails Rumble?
The Rails Rumble is an annual 48 hour web application development competition in which teams of skilled web application developers get one weekend to design, develop, and deploy the best web property that they can, using the power of Ruby and Rails.
And it sure as hell looks as tight as we could have hoped for.
Since we only had 48 hours to make it, we didn’t get in as much features as we wanted but all the basics are there.
- Did we have fun? Yes.
- Will we participate again? Yes.
- Are we proud of our 13th place? Yes. (13 is the new 1)
- Did we sleep much? No. (OK: I overslept for 6 hours; quit whining Jelle)
- Did it have bugs? Yes, some.
- Could we have done it without Rails? No. (Okay, maybe in Sinatra)
- Will we push the app even further? Yes. (It got an update last night, which fixed some bugs and made some performance enhancements).
How does it work?
After you sign up with Natter, it will listen to your tweets and post them to Facebook, enhancing them to atual Facebook posts if they contain an URL or an image.
Now, dear reader, I can tell you’re not impressed but here’s the kicker; whenever a Facebook-user posts a comment to a Natter posted tweet on Facebook, Natter will post this comment on Twitter. And, you can reply to it, straight from the confines of your sweet minimal (before they redesigned) Twitter.
So now you’ve gone full circle for al your social networking needs. (Unless you’re one of thos Jaiku or IRC guys)
Yeah, but how does it work?
We couldn’t have done it without standing on some pretty big shoulders; so we’re using (like most of our webaps) Rails coupled with the following gems: twitter_oauth for handling all things Twitter, facebook_oauth for all things Facebook, bitly to make the Facebook links small and kinda sexy (I’m stretching the limits of sexy here, it’s more javacool and to make sure the server can handle it all we’re using Resque for queuing all background jobs (pretty much everything is a background job).
The whole thing is hosted on a, Rails Rumble supplied, Linode slice which also hosts our MySQL db. We’ve only brought it down once, and that was pretty much our own fault (note to future self: decide who puts the indexes on the DB), so it’s safe to say Linode rocked.
What did we learn?
- A sexy design works
- A painless signup process works
- Sleep is overrated
- We need to prepare better (Texts, flow of the app, design sketches,…)
- Testing stuff like this was hard. (Study up on how to test your app)
- Hard deadlines work (We could only submit our final build 10 minutes before the deadline)
- Sysadmin-skillz (or rather devops /ht @frank_be) are needed
- Being able to split up your app in tasks suited for one person, is a plus.
I finally found some time to write my first blog post for my awesome company! Here we go…
For a recent project I decided to use PDFKit to generate PDF files in Ruby on Rails. I’m used to generating these with Prawn, but as we all know it can be very hard to get it styled correctly.
First things first, we have to install the gem and then the wkhtmltopdf binary.
Installing the gem is easy:
gem install pdfkit
Hey, installing the binary is as easy:
sudo pdfkit --install-wkhtmltopdf
Now we can use PDFKit on our system.
To generate a pdf from a HTML web page just create the kit instance:
And then convert it to a pdf file:
You can also pass an URL of a file to the PDFKit initializer method:
Some Rails magic
But here is some real magic. You can just attach the pdf format to your controller actions. When you go to http://test.be/people/jelle.pdf, the PDF will be downloaded (or your browser will open it in a window).
Just include the following lines of code in your environment.rb (for Rails 2.x) or application.rb (for Rails 3.x):
What happens now is that your show.html.erb file is rendered as usual but it is converted to the PDF format afterwards. So the PDf file gets the same style as the HTML page (with CSS styling included).
I used this for a project, and it works great!
Changing the filename
Now another handy way to change the filename for the PDF file. When you go to http://test.be/people/1.pdf, the downloaded filename will be 1.pdf. But what if I want the filename to be jelle-vandebeeck.pdf? Well, just include this line of code somewhere in your action:
For more information visit the Github page.
When you’re building webapps, sooner or later you run into a screen where you want to list some data. An index page of all people for instance. There are many other cases where you want a lot of data, but for now, let’s follow this path.
Say, you got thousands of people, you’re not going to show them all on one page, do you? Probably you will use something like will_paginate.
Say, you want to see this pagination stuff in action, in your development environment. Since you are not going to manualy create a couple of thousand people, one option could be to set the number of results per page to 1 or 5 or 6 or … well you know what I mean.
This will create 3000 people with the name John Smith. It becomes better when you throw in the faker gem.
This will create 3000 people with random names, which is better if you ask me.
If you’re a developer, you know how it goes. At the beginning of a project, you draw some UML sketches to get you going. There’s a nice tool to do that. As your project goes on, and your code grows, these diagrams don’t get updated too often. After all, every piece of knowledge must have a single, unambiguous, authoritative representation within a system. And for a class model, that representation is the code.
But at some points during your development, you have to communicate with some stakeholders. They want to know what your class model looks like. So you have to provide them with a drawing. Until recently, we went through the trouble of creating these diagrams manually. But those days are over: we found yUMLmeRails
To get yourself a diagram, follow these steps:
Your diagram will be saved in your_project/diagrams.
Easy. Nice. Handy. Did I mention ‘pie’ ?
It gets even better. By passing a hash to the finder you can initialize or create a new object with all the values while only the attribute named in the finder will be used to find the object.
Remember when we talked about how to use multiple versions of Ruby on your Mac?
Well, apparently that post is already outdated. Now all you have to do is to install the rvm gem
I’ve just tried it, and it works
without a hickup. I’ve noticed one little thing: the installation of the gem creates a file
~/.bash_profile. This, apparently, overrides
~/.bashrc on my Mac. I lost all of my neatly-crafted terminal environment. Deleting
~/.bash_profile fixed this. Happy camper!
If this isn’t a discovery! I couldn’t have said it better myself.
Just like Nick Quaranto, I prefer positive conditions. For example, you’ll find more
unless object.blank? in my code than
if !object.blank?. So I’m very happy that I learned about
present?, the inverse of
For a while, I’ve felt an itch to experiment with Ruby 1.9. It would be better to use it in our day-to-day work, but Ruby 1.9 isn’t that far yet. What has hold me back until now, is that I don’t want to make an unstable Windows machine out of my Maccie. It’s my primary workstation, and I have to work on it constantly.
With the help of the RubySwitcher script, it is laughingly easy to use several Ruby versions next to one another. There’s no more excuse not to do it.
Out of the box, you can install and use the following versions of Ruby:
To use the standard Ruby version:
Rails Engines, what are they? What do they do? But most importantly: how do they do it?
Rails 2.3 brings us much of the same functionality as the Rails Engines plugin. Learn how to embed one application into another in this episode.
Engines allow us to use one application in another in the form of a plugin. As the screencast shows, you can integrate the
app folder of a Rails application in the plugin of another one. All models, controllers and views are available. If you still need custom functionality, you can add them in your applications ‘own’
app folder by redefining the model, controller or view. The same goes for routes; if you have
routes.rb in your plugin dir, it is loaded as well.
And i18n, how about that? You would expect
config/locals/*.yml to work just as nicely as the
app dir and
routes.rb. But it doesn’t.
Luckily, it’s not that hard to solve:
environment.rb, you add
In the hope that the Google Gods will help me next time I need this :-)
An easy way to generate a human-readable timestamp string, following the ISO 8601 standard, is:
Very handy if you need a timestamp in a file you’re writing.
Today I’ve pushed a few updates to the Roleify rails plugin.
The changes are
- you can now use ‘namespaced’ controllers
- I added a helper method to hide/show blocks for a specified role
role_a refers to a
role_b refers to an
More info on GitHub.
What is it?
Invokes the method identified by the symbol method, passing it any arguments and/or the block specified, just like the regular Ruby Object#send does. Unlike that method however, a NoMethodError exception will not be raised and nil will be returned instead, if the receiving object is a nil object or NilClass.
What did you just say?
The code above normally throws an exception if
someobject is nil. By using
try it just returns
Don’t overuse this.
What do you need?
Next, open RubyMine’s
Find the line with value
1.5* en change it to
Start RubyMine and check the ‘about’. It should look something like this:
Enjoy. It does feel snappier on my side.
On a sidenote: You can set the default Java version via the ‘Java Preferences’ app.
Sign up for our newsletter
Who’s got the mic?
Architecture & Design
Koen Van Der Auwera
Bob Van Landuyt
Nathan de Witte