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. 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. Follow steps 1-3 of this tutorial, if you need help setting this up: Initial Server Setup on Ubuntu 14.04 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 Install Ruby. How To Deploy a Rails App with Unicorn and Nginx on Ubuntu 14.04. Introduction When you are ready to deploy your Ruby on Rails application, there are many valid setups to consider. This tutorial will help you deploy the production environment of your Ruby on Rails application, with PostgreSQL as the database, using Unicorn and Nginx on Ubuntu 14.04. Unicorn is an application server, like Passenger or Puma, that enables your Rails application to process requests concurrently. As Unicorn is not designed to be accessed by users directly, we will use Nginx as a reverse proxy that will buffer requests and responses between users and your Rails application. Prerequisites This tutorial assumes that you have an Ubuntu 14.04 server with the following software installed, on the user that will deploy the application: If you do not have that set up already, follow the tutorials that are linked above.

Also, this tutorial does not cover how to set up your development or test environments. Create Rails Application rails new appname -d postgresql cd appname \q vi .rbenv-vars. 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. 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. Here is an SQL statement: Similarly, for this CSS statement: 1. 2. 3. Ruby on Rails ActiveRecord Associations - The Lesser Known Parts. ActiveRecord associations are part of a Rails developer’s basic toolbox. 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. 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 It may be that you want to have options to display all comments on a post, or restrict the comments to just those created today. Now, in addition to being able to call my_post.comments you can refine the records retrieved by calling my_post.comments.today Callbacks Inverse Of Huh! The Detailed Guide on How Ajax Works with Ruby on Rails. This is a tutorial for ajax use with Rails conventions. 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 Before we start, let’s take a quick look at our schema so that we know what we’re working with: After creating a Task model and then creating some tasks to play with, our Task Controller should look like this: Next, create new.js.erb: 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. 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: What do we do? In this article we will cover automated deployment of a Ruby on Rails application to a VPS (Virtual Private Server). We will install a Ubuntu Linux Server (14.04 LTS) and prepare our project for automated deployment with Capistrano 3. We will use DigitalOcean as a VPS server provider, because they have good technology with competitive prices.

The Automated Deployment Process with Capistrano For web applications, deployment means a sequence of steps to enable a feature on the production server after having built it locally. Capistrano is a program that allows developers to automate the deployment process using a Ruby based script. Step 1. Now copy it to “SSH Keys >> Add SSH Key” with DigitalOcean: Step 2. Step 3. Chef basics for Rails developers. Chef is a cloud infrastructure framework. It is a tool that allows us to manage configurations, similar to Puppet and a few other tools, but Chef is my favorite because is written in Ruby.

Chef can help you manage your infrastructure dependencies, create folder structure (with ‘knife’) and bootstrap our entire system or update configurations with just a few commands. We’ll walk through the process of setting up a Ruby on Rails server deployment with Chef with this tutorial. We are going to build this tutorial on a brand new Ubuntu/trusty64 server. I have created the following repositories to go along with this tutorial. Our target is: over a Ubuntu/trusty64 brand new server, we want to configure it and install our Rails Application. For you to have example projects to go with all the instructions written here. Chef-basics as our project of infrastructure with Chef/Vagrant. Step 1. Prerequisites: Install VirtualBox.

We will need a infrastructure project folder: And just install the gems: How to Build Authentication in Rails. Authentication is the process of establishing, and subsequently confirming, a site user’s identity. 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. There are a few variations on this theme, notably the authenticate_with_http_basic method, which yields the given username and password to a block, which returns a true value if the supplied credentials are valid. However, there are a number of security vulnerabilities in using these methods, to say the very least. Some other disadvantages are: 2. Storing passwords Here’s an example: Let’s see the same code using bcrypt. 3. Gotealeaf. Devise is a popular authentication solution for Rails applications.

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 also provides a hook so your app can deal with users who aren’t currently logged in. Warden knows nothing about the existence of your Rails app. 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.

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. Ruby on Rails Guide states “with polymorphic associations, a model can belong to more than one other model, on a single association”. Basics While writing some rails application you will run into situations when you have model associations that seem to be similar, for example lets assume you have Course and Lab models in your application.

Lets see how to implement it. Here ta_duty_id is a foreign key and ta_duty_type will tell model TeachingAssistant which model it is associated with. /db/migrate/********_create_teaching_assistants.rb Now our model is generated, to run the migration do rake db:migrate. /app/models/course.rb. 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. 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. Running Rails Application Code via Cron Rake Tasks from Cron Runners Debugging Custom Code The Whenever Gem Pitfalls of Crontab Sidekiq and Sidetiq. 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. It can also have undesirable ramifications with regard to security and performance. Accordingly, while Rails is easy to use, it is also not hard to misuse.

Common Mistake #1: Putting too much logic in the controller.