background preloader

Best practices rails

Facebook Twitter

How To Install Ruby on Rails with rbenv on Ubuntu 14.04. Introduction Ruby on Rails is an extremely popular open-source web framework that provides a great way to write web applications with Ruby.

How To Install Ruby on Rails with rbenv on Ubuntu 14.04

This tutorial will show you how to install Ruby on Rails on Ubuntu 14.04, using rbenv. This will provide you with a solid environment for developing your Ruby on Rails applications. rbenv provides an easy way to install and manage various versions of Ruby, and it is simpler and less intrusive than RVM. This will help you ensure that the Ruby version you are developing against matches your production environment. Prerequisites Before installing rbenv, you must have access to a superuser account on an Ubuntu 14.04 server.

When you have the prerequisites out of the way, let's move on to installing rbenv. Install rbenv Let's install rbenv, which we will use to install and manage our Ruby installation. First, update apt-get: sudo apt-get update. How To Deploy a Rails App with Unicorn and Nginx on Ubuntu 14.04. Introduction.

How To Deploy a Rails App with Unicorn and Nginx on Ubuntu 14.04

Ruby Programming: Declarative Thinking with Higher Order Functions and Blocks. Here’s an question we ask as part of our Ruby on Rails Bootcamp Program: “Pick out all the odd numbers from the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and print them out.

Ruby Programming: Declarative Thinking with Higher Order Functions and Blocks

Typically we get two types of solutions from our students. solution 1: solution 2: Experienced Ruby developers will say that solution 2 is more “idiomatic Ruby” – it’s shorter, easier to read, and more “intention revealing”, meaning that you can easily determine what the code does by its appearance. The fundamental difference of the two solutions, however, is that solution 1 focuses on the “steps of implementation” or the “how”, which is typical for the imperative style of programming; whereas solution 2 follows the declarative style of programming, using a Ruby block to make a declaration / expression of the problem itself – selecting odd numbers. To understand what declarative programming is, look no further than SQL and CSS.

Ruby on Rails ActiveRecord Associations - The Lesser Known Parts. ActiveRecord associations are part of a Rails developer’s basic toolbox.

Ruby on Rails ActiveRecord Associations - The Lesser Known Parts

But not all are aware of some of the extra freatures that are available when defining associations. Customizing Queries Perhaps you’ve developed a blogging site that allows comments. Some comments may be inappropriate (it is the Internet, after all) so you want to ensure comments are only shown if they’ve been approved by your site moderator. You’ve added a boolean field to your comments table, :approved.

Since for each blog post you only want to bring back a list of approved comments (only the ones the moderators have flagged as approved) you can customize the query You now have an association my_post.approved_comments that you can use wherever you need to retrieve a post’s vetted comments. Extensions. The Detailed Guide on How Ajax Works with Ruby on Rails. This is a tutorial for ajax use with Rails conventions.

The Detailed Guide on How Ajax Works with Ruby on Rails

For illustrative purposes, we’ll build a single-page task list app. For reference and convenience, I have created 2 github repos: Static Task List: A textbook example on creating a basic and static task list. It also serves the purpose of demonstrating an app with excess navigation that can be significantly improved with ajax for an enhanced user experience. Rai-jax: This tutorial serves an illustrative example of the significant improvement a little ajax and some styling can provide to an otherwise dull and static app. About Ajax Ajax (Asynchronous JavaScript and XML) is used as a mechanism for sending and retrieving data asynchronously (in the background). Part One: Creating a New Task on the Index Page. Deploy Rails Applications with Capistrano 3. Well, we have a well-developed application built with our laptop, but we want it to be seen by the entire world.

Deploy Rails Applications with Capistrano 3

Even better, we want to automate this process in order to push our new super-duper features in seconds with only one command on our development machine: Chef basics for Rails developers. Chef is a cloud infrastructure framework.

Chef basics for Rails developers

How to Build Authentication in Rails. Authentication is the process of establishing, and subsequently confirming, a site user’s identity.

How to Build Authentication in Rails

It is how an app recognises, who you are. In this article, we’ll go through a few methods that you can add authenticate to your Rails application. We’ll start with the HTTP Basic Authentication, look at the most commonly used username and password based local authentication, and then some less known alternatives. 1. HTTP Basic Authentication In HTTP Basic Authentication a user’s credentials are stored in a HTTP header field, and are sent as a part of each (HTTP) request. Gotealeaf. Devise is a popular authentication solution for Rails applications.

gotealeaf

It provides a full gamut of features, and can be configured to meet all, but the most unusual, requirements. In this tutorial, we are going to look at how Devise is implemented on a high level, go through configueration and common use patterns of the gem, how to set up authentication with Devise, and finally demostrate how to write tests with Devise. Devise and Warden The Devise gem is built on top of Warden. Warden is a Rack application, which means that it runs as a separate and standalone module, and is (nearly always) executed before the chief Rails application is invoked. Warden provides the cookie handling that verifies the identity of a logged in user via a (secure) session string, in which the id (primary key) of a particular user is somehow stored and disguised. Warden knows nothing about the existence of your Rails app. Devise often interacts with Warden using Strategies. Understanding How Devise Works Testing. Understanding Polymorphic Associations in Rails. Polymorphic associations can be tricky to understand and many times one can get confused about when to use them, how to use them and how they are different from other associations.

Understanding Polymorphic Associations in Rails

In this article we will go over these issues. Introduction Active Record Associations are one of the most important features of Rails. Polymorphic association is part of these associations. Cron Jobs and Rails. Cron If you’re reading this article, it’s probably because you’ve heard of cron jobs, cron tasks, or crontab. Cron is a piece of software written for *nix-type operating systems to help with the scheduling of recurring tasks.

You may want to use cron to schedule certain recurring actions in your Rails application, such as checking each day for expired accounts or clearing out expired sessions from your database. It’s pretty easy to start working with cron jobs. You can start editing your cron tasks using the crontab command: This will open up a text file in your terminal’s default editor (probably vim or nano). Remember that your scheduled tasks will be run as the user that you use when you invoke crontab. Once you’re in the editor, you can start creating and editing cron tasks. The parts represent, in this order, the minute(s), hour(s), day(s) of the month, month(s), and day(s) of the week to run the command. The Twelve-Factor App.

10 Most Common Rails Programming Mistakes. Ruby on Rails (“Rails”) is a popular open source framework, based on the Ruby programming language that strives to simplify and streamline the web application development process. Rails is built on the principle of convention over configuration. Simply put, this means that, by default, Rails assumes that its expert developers will follow “standard” best practice conventions (for things like naming, code structure, and so on) and, if you do, things will work for you “auto-magically” without your needing to specify these details.

While this paradigm has its advantages, it is also not without its pitfalls. Most notably, the “magic” that happens behind the scenes in the framework can sometimes lead to headfakes, confusion, and “what the heck is going on?” Types of problems.