Tag: Ruby

  • Faking it: enums in Ruby

    Ruby doesn’t have enums and in Rails I sometimes need it. I’ve come out with my own way of doing after some trial and error. First I want to be able to access these enums as constants in a class, like: That’s actually quite easy: If I’m storing those values on the database, I’d like…

  • Metaprogramming Ruby

    Metaprogramming Ruby

    There are thousands of books that will take you from illiterate to novice in any programming language. But finding those that will take you from novice or intermediate to expert is hard. I remember reading Effective Java some years ago and wishing I had something like that for Python. I’ve never found one. Metaprogramming Ruby…

  • The magic of Bundler

    Recently I reported a bug for Formtastic. Justin French, the author of Formtastic, created a branch and made a fix. He then asked me for my feedback. I look at the code and then decided to give it a try. In a pre-Bundler world that would have required this: Find a directory to play with…

  • Redirecting back

    It’s very common in Rails CRUD to have a create and update actions that redirect back to the show action. The idea is that you show an object, click edit, save, go back to showing said objects with your changes. All is fine until you have an edit link somewhere else. Let’s say you have…

  • Generating sample data

    As I’ve said in previous posts, I like being able to generate sample data for the projects I’m working on very quickly. It allows new developers to get up to speed fast, and new developers to move faster. When I don’t have a sample data generation method, I’m always scare to try whether, for example,…

  • Deleting all records in a Rails project

    During the initial phase of development of a Rails application I don’t use migrations as migrations but as table definitions. Until I deploy I feel free to modify the migration files as much as I want and I have one per table. The downside of that is that the only way to apply the changes…

  • undefined method `authenticate?'

    If you are getting this error: ActionView::Template::Error: undefined method `authenticate?’ for nil:NilClass in your call to Devise’s user_signed_in? or similar, you probably forgot to add this: class ActionController::TestCase include Devise::TestHelpers end at the bottom of the test_helper.rb file. Not that that would ever happen to me…

  • Hashes for select

    In Ruby on Rails there’s a very easy way to create a select tag: form.select(“period”, [[“Month”, 1], [“Year”, 12]]) In my case I have the options in a hash, like: periods = { 1 => “Month”, 12 => “Year” } but when I did this: form.select(“period”, periods) I was surprised to find out that the…

  • Show a devise log in form in another page

    Show a devise log in form in another page

    Update: there’s a new version of this post covering Devise 4.0.0: Show a devise log in or sign up forms in another page Devise create various forms, among them one for signing up and one for logging in of course. These are the forms as they are generated in Devise 1.0.8: and If you try to…

  • My first contribution to Rails

    https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4809 Looking forward to many others.