RailsApps

Aug 21

Error Installing Rails 4.2.0.beta1

Rails 4.2.0.beta1 is out with some great new features such as Active Job. Unfortunately, there’s a bug with the newest 2.4.1 release of RubyGems (the system gem) that makes it difficult to install Rails 4.2.0.beta1. See Rails issue 16609.

Check if you are using RubyGems 2.4.1:

$ gem -v
2.4.1

Do you encounter this error when installing Rails 4.2.0.beta1?

$ gem install rails --pre
ERROR:  While executing gem ... (Gem::DependencyError)
    Unresolved dependency found during sorting - activesupport (>= 4.0)
(requested by sprockets-rails-3.0.0.beta1)

You can work around the problem by downgrading to the 2.2.2 version of RubyGems.

$ gem update --system 2.2.2
Updating rubygems-update
Fetching: rubygems-update-2.2.2.gem (100%)
Successfully installed rubygems-update-2.2.2
Installing RubyGems 2.2.2
RubyGems 2.2.2 installed

Now you will be able to install Rails 4.2.0.beta1.

If you are using RVM, and you want to use two different versions of the RubyGems system gem, you’ll need to install two different versions of the latest Ruby 2.1.2 release with different RVM names. For example, if you already have Ruby 2.1.2 installed with the newest RubyGems 2.4.1, you can install another version of Ruby 2.1.2, giving it a different name:

$ rvm install ruby-2.1.2-oldrubygem

RubyGems 2.2.2 is installed by default with the current Ruby 2.1.2 release:

$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
$ gem -v
2.2.2

You’ll be able to install Rails 4.2.0.beta1.

Aug 20

‘Learn Ruby on Rails’ Free for Students

Do you know someone who teaches a class or workshop on Rails?

I offer my book for beginners, Learn Ruby on Rails, to students enrolled in classes or workshops, including university and community college classes, immersive code camps, and community workshops. The book is popular with instructors and students. Here are some of the places where the students have received the book for free:

If you know someone who teaches Rails, they can email me at daniel@danielkehoe.com for an evaluation copy of the book. For an independent review, see Tech Book Face Off: Learn Ruby on Rails. The book is free for students enrolled in any organized class or workshop. If you’re studying on your own, organize a study group and get in touch with me. Or purchase a RailsApps subscription for the book plus many more in-depth tutorials.

Aug 15

RailsApps Update for Devise 3.3

Devise 3.3 was released on August 13, 2014. The Devise CHANGELOG lists several minor enhancements and bug fixes. In the new version, Devise default views now say “Log in” instead of “Sign in.” Alex Soble proposed the change, based on an article that points out the easy confusion of “Sign up” and “Sign in.” What do you think? Was the change warranted?

Perhaps a more significant change is new wording for the error messages when a user’s email address or password is incorrect. If you’re using an application that includes feature tests for sign in (oops, “log in”) with Devise, and you update the Devise locale file, your feature tests will break. Take a look at the changed locale file if you’d like to see the new error messages.

Several of the RailsApps example applications use Devise and provide RSpec tests for sign up and sign in with Devise. I’ve updated the RailsApps example applications to use Devise 3.3 and revised the tests so they continue to work. The aim of the RailsApps project is to provide a reliable reference implementation for integration of the gems that developers are most likely to use in a new Rails application. With this update, you’ll get the most recent version of Devise when you use Rails Composer or copy an example application.

The RailsApps project is supported by our monthly subscribers. If you like the work we’re doing, please consider joining and supporting the project with a subscription.

Aug 14

Rails Bootstrap Update

Bootstrap 3.2 was released on June 26, 2014 and the bootstrap-sass gem was released immediately afterward. Bootstrap 3.2 contains many new features and bug fixes but the biggest impact for Rails developers are new installation instructions for the bootstrap-sass gem. Previously, developers added Bootstrap to the Rails asset pipeline by adding to the app/assets/stylesheets/application.css.scss file:

@import "bootstrap";

Now the recommendation is to use:

@import "bootstrap-sprockets";
@import "bootstrap";

This will only matter to you if you are using glyphicons. See details if you’d like to know more.

The installation instructions are also different for the Bootstrap JavaScript components. In the file app/assets/javascripts/application.js we had:

//= require bootstrap

Now we can use:

//= require bootstrap-sprockets

The older version delivered concatenated JavaScript. The newer version uses a require for all Bootstrap modules, making output easier to debug.

Most Rails developers who are using Bootstrap are unaware of these changes, unless they’ve recently reviewed the bootstrap-sass README. It’s an example of a typical open source problem. An obscure bug gets fixed by changing how a gem gets integrated into an application but developers don’t learn they need to update their applications to accommodate the fix. That’s where the RailsApps project provides a service. I’ve updated every RailsApps example application that uses Bootstrap, so if you clone a repository or use Rails Composer to create a starter app, you’ll have the correct configuration for Bootstrap. You can use the RailsApps example applications as a reliable reference implementation, so you can easily see how Bootstrap or other gems should be integrated with Rails.

Aug 09

Rails Authentication With OmniAuth

I’ve completed a new tutorial on Rails authentication with OmniAuth.

RailsApps subscribers are getting tutorials every month:

You can join RailsApps to get the tutorials and support the project.

OmniAuth Tutorial

I’ve already written a tutorial about Devise, the most popular authentication gem. With Devise, users sign up with an email address and password to gain access to your web application. With OmniAuth, visitors sign in using an account they already have with Twitter, Facebook, GitHub, or many other social networking sites. If you are building an application that is intended for users of one of these social networking sites, you’ll probably want to use OmniAuth, and the new tutorial shows you how.

In the tutorial, I first show a simple way to implement authentication. Then, in the chapter “Customizing the Application,” I show how to request an email address from the user when he or she first signs in to your application. Some service providers (Twitter, Meetup, LinkedIn) do not provide an email address when their users sign in to your application using OmniAuth. The lack of an email address can be a business drawback, if you want the opportunity to stay in contact with the user by email, so the tutorial shows how to solve the problem.

Jul 15

Analytics for Rails Composer

Now, when you build a Rails starter application with Rails Composer, you get the option of page-view tracking without any effort. Run Rails Composer and you’ll see:

recipe  Running analytics recipe...
option  Install page-view analytics?
    1)  None
    2)  Google Analytics
    3)  Segment.io
choose  Enter your selection: 2
option  Google Analytics ID?

You get a choice of either Google Analytics or the Segment.io service.

Google Analytics

With the release of Rails 4.0 and Rails TurboLinks, it’s been difficult to set up Google Analytics in a Rails application. Rails Composer solves the problem with a brilliant JavaScript implementation from Jonathon Wolfe. Rails Composer installs Jonathon’s tracking script in the Rails asset pipeline and sends tracking data to Google Analytics whether or not TurboLinks is enabled in your Rails application.

Segment.io

I like Segment.io because it gives me Google Analytics plus any of 107 other services. With Segment.io, you can instantly switch on tracking for services such as Mixpanel, KISSmetrics, and many others. It’s a commercial service but it’s free for low-traffic sites (less than 100K pageviews or events per month). Rails Composer installs the Segment.io tracking script in the Rails asset pipeline, and adds code to accommodate Rails TurboLinks.

Rails and Analytics

I wrote an article on Analytics for Rails last year. You can see the article for more about the code you need for analytics. My goal for Rails Composer is “no mystery code.” That’s why I write tutorials and articles to explain the code generated by Rails Composer. Thanks to support from RailsApps subscribers, I was able to update the article this week. Please join me in giving a shout-out of thanks to the subscribers who pay $19/month to make this possible. And if you haven’t joined, please consider getting a subscription to support the project.

Mailing List

There’s also a new mailing list for news about Rails Composer. Join the mailing list if you want announcements of new features for Rails Composer.

Jul 06

Mailing List for Rails Composer

You can now join a mailing list for news about Rails Composer. Sign up here:

Expect to receive two or three emails a month with announcements about features and changes.

The next time you run Rails Composer to build a starter application, you’ll see an option to join the mailing list. I’ve built an application for mailinglist.railscomposer.com (which you can find on GitHub) with a simple API that responds to a curl request generated by Rails Composer. It’s fun to see it in action. From the Rails Composer command line you can enter your email address, and the mailinglist.railscomposer.com application updates a MailChimp mailing list. Rest assured, there’s no funny business going on with the email addresses (it’s open source so anyone can examine the code); email addresses are not stored in the application database; they are simply passed to MailChimp.

I’m eager to see how many developers are using Rails Composer. In just a weekend, 20 developers have signed up for the mailing list. The Rails Composer repo on GitHub has over 2000 stars but we don’t really know how many developers are actively using Rails Composer. In any case, the mailing list will be a better channel than blog posts or tweets to announce Rails Composer features and changes.

If you’ve got ideas for Rails Composer, let me know. You can reach me at (daniel@danielkehoe.com). Thanks to support from subscribers, I’m now working full-time on the RailsApps project, splitting my time between developing Rails Composer and writing tutorials.

Jun 26

Locale Recipe for Rails Composer

Thanks to a code contribution by a developer in Russia, Rails Composer now has an option to set a default locale for a starter application. Anton writes, “I know it isn’t a significant new feature, but maybe the ability to set a default locale would be handy for developers from non-English-speaking countries.”

Rails Composer saves one step for a developer by setting the default locale in the config/application.rb file and creating an empty file in the config/locale/ folder. As a bonus, if the application uses Devise for authentication, Rails Composer will add the devise-i18n localization gem. That means your Devise messages are automatically translated for 40 different languages. Cool!

It would be nice if we could automatically translate the English words on the default Rails Composer home, user, and about pages. To do that, we’d have to add translations to the rails_apps_pages gem. That’s not in my plans, but if anyone wants to do it, I’ll add the contributed code.

Right now, the “set a default locale” option is available when you select “Custom application” in Rails Composer.

Jun 20

MongoDB Rescue Operation

In April I wrote of my plans to drop MongoDB (and the Mongoid gem) from Rails Composer (see MongoDB Dropped From Rails Composer). Today I received a note from a developer who makes a case for keeping MongoDB. Steven Kolstad writes:

I believe that MongoDB has earned its place in our development community, even if the honeymoon period of early adopters is starting to wane. Despite what public opinion is gossiping about currently, there is a use case for this technology, and I predict that once the marketplace is educated about what that is, the popularity will balance itself out. I sat and watched how long it took for Ruby to prove that it wasn’t some fad. That, even amidst wide speculation to the contrary, Ruby could be an enterprise development tool with real production value. I feel that MongoDB will likewise prove their worth in the arena.

If, like Steven, you’re interested in adding back MongoDB and Mongoid to Rails Composer, let me know (daniel@danielkehoe.com) and we’ll put together a team to share the effort.

I just released a 3.0.0 version of the rails_apps_composer gem that cleans up a lot of gnarly code, making it easier to add new recipes. The gem is still challenging to figure out, but I can help you with it. The rails_apps_composer gem contains the recipes that build the Rails Composer tool.

As a first step, someone could modify the rails-omniauth example app for Mongoid. It is the simplest of the example apps that use a database. The job may be as simple as modifying the User model. If someone builds a rails-mongoid-omniauth application, I’ll add it to the RailsApps examples collection.

It is important to have an example app because the only way to test Rails Composer with a regression test after each new release is to build an example app and compare it to the example app, using a file compare tool. Mark Blackwell contributed a regression testing tool named megatest, which someone may be able to adapt as needed.

To add Mongoid to Rails Composer, you can examine the file recipes/rails-omniauth.rb (in the rails_apps_composer gem) and create a new recipe recipes/rails-mongoid-omniauth.rb to build the example app.

That will give you a recipe that builds an example app but it won’t add Mongoid as a “custom build” option. That requires finding ways to create a Mongoid version of each existing RailsApps example app. You will have to find each place that a models/user.rb file is created and add logic to modify for Mongoid.

If you like MongoDB and Mongoid, this is your chance to join an open source team and build something that makes life easier for numerous developers. Don’t hesitate, even if you’ve never contributed to an open source project before.

Jun 18

Rails Authorization With Pundit

I’ve completed a new tutorial on Rails authorization with Pundit.

RailsApps subscribers are getting tutorials every month:

You can join RailsApps to get the tutorials and support the project. Coming next is a tutorial on OmniAuth.

Role-Based Authorization

The new tutorial covers role-based authorization, showing how to use the Active Record enum feature in Rails 4.1 to add a role attribute to a User model. You can set up roles for administrators, users with free or premium plans, or any other system of privileges. The tutorial shows how you can set up simple role-based authorization without any extra gems. For more complex applications, the tutorial introduces the Pundit authorization gem.

Pundit or CanCan?

The CanCan authorization gem has been popular since Ryan Bates released it four years ago (it’s been recently replaced by its successor, CanCanCan). CanCan provides a domain-specific language that isolates all authorization logic in a single Ability class. As an application grows in complexity, the CanCan Ability class can grow unwieldy. Every authorization request requires evaluation of the full CanCan Ability class, adding performance overhead. Like CanCan, Pundit offers the advantage of segregating access rules into a central location. In Pundit, it’s a folder named app/policies/ containing plain Ruby objects that implement access rules. Pundit policy objects are lightweight, adding authorization logic without as much overhead as CanCan. Pundit is well-suited to the service-oriented architecture that is growing in popularity among Rails developers, emphasizing object-oriented design with discrete Ruby objects providing specialized services.

In-Depth Pundit Tutorial

For a small gem, Pundit has a surprising number of features. The RailsApps tutorial goes into depth, covering:

It’s the only in-depth guide to Pundit. I appreciate the support from RailsApps subscribers that made it possible to release this tutorial.