Tag: ruby on rails web development

Transactional callbacks in Ruby on Rails

There are two additional callbacks that are fired when a database transaction completes: after_commit and after_rollback. These callbacks are very similar to the after_save callback, except that they are not fired until the database changes have been committed or reversed. They are most useful when your Active Record models need to interact with external systems […]

Modifying Existing Migrations in Ruby

Periodically, you will make mistakes when writing migrations. If you’ve already run a migration, you can’t just edit the migration and run it again: Rails will assume it has already run a migration and will do nothing when you run rails db:migrate. You must rollback the migration (using bin/rails db:rollback, for example), edit the migration, […]

Using reversible in Ruby

A complex migration may involve processes that Active Record does not know how to reverse. You can use reversible to specify what to do when a migration is run and when it needs to be rolled back. For example: class ExampleMigration < ActiveRecord::Migration   def change     create_table :distributors do |t|       t.string :zipcode     […]

Model Generators

The model and scaffold generators will generate migrations suitable for creating a new model. The migration will contain instructions for creating the appropriate table. If you tell Rails which columns you want, expressions to add those columns will also be generated. For example, running: $ bin/rails generate model Product name:string description:text will create a migration […]

Naming conventions in RoR development

By default, Active Record uses some naming convention to know how the relationship between models and database tables should be created. Rails pluralizes class names to find the appropriate database table. For example, for the Book class, create a database table named books. Rails’ pluralization mechanisms are very powerful, being able to pluralize (and singular) […]

Safety during RoR development

(Basic Authentication) Basic authentication If you publish your blog online, anyone can add, edit, and delete articles or delete comments. Rails provides a very simple HTTP authentication system that works well in this situation. In the ArticlesController we need a way to block access to various actions if the user is not authenticated. Here we […]

Ruby on Rails model which process comments on articles

Generating a Model We intend to use the same generator that we used earlier when creating the Article model. This time we will create a Comment model containing a link to the article. Run the following command in a terminal: $ bin/rails generate model Comment commenter:string body:text article:references This command generates four files: File Destination […]

Article updates on website built with Ruby

We have revealed the “CR” part of CRUD. Now let’s focus on the “U” part, updating articles. The first step is to add an edit action to the ArticlesController, usually between the new and create actions, as shown. def new   @article = article.new end def edit   @article = Article.find(params[:id]) end def create   […]

Displaying articles on Ruby on Rails website

We still need a way to display a list of all our articles, let’s make one. The route displayed with bin/rails routes is the following: articles GET /articles(.:format) articles#index Add an index action corresponding to this route inside the ArticlesController in the app/controllers/articles_controller.rb file. When we write an index action, it’s common practice to place […]