How to implement dependent dropdowns in Rails 4 with an unobtrusive jQuery script

I recently had to implement a view with the classic search form with two select dependent on each other. My aim was to leave the page as clean as possible and make the code that loads the options for the daughter dropdown reusable.Suppose, therefore, to have two models, SpecializationType and Specialization, defined as follows:1234567891011121314151617181920212223242526272829303132class SpecializationType ...

Read more

Tips: Multiple pagination with will_paginate

Will Paginate is a great and widely used gem for pagination in Ruby on Rails.Sometimes you need to paginate multiple list on the same page.This tip show you how to put a list of paginated products and a list of of paginated services on the same page with will_paginate. The key point is to use a custom param param_name in the view and also in the controllerThe view code is something like this:123# ... # products list here <%= will_paginate @produc...

Read more

How to send email asynchronously using Devise and Rails3

Hello everyone.I’d show a workaround to send email asynchronously using Devise and Rails3.Suppose we have already up and running our application with Devise and delayed_job correctly installed.A first attempt was to add in config/initializers the following file (devise_async.rb):12345678910111213141516#devise_async.rb module Devise ...

Read more

Rails Best Practices 6: Filters and helpers file

With the today’s post I shall conclude my series on Ruby On Rails Best Practices . This is not because the required topics are completed, but rather because after the release of Rails 3.0, some constructs have been changed and should be reviewed. Probably in future we will return to talk about best practices, but s...

Read more

Rails Best Practices 5: Optimize Migration

Migrations, in my opinion, are one of the best things in Rails since these allow the creation and populating the database using ruby code without having to worry about which type of db run below. That said, even writing the migration is better to follow some best practices. 1. DB Index The first practice I strongly recommend is to define indices for the external keys and for all those columns on which you will make sort, search and groups. Let’s take a sample migration:123...

Read more

Rails Best Practices: introduction

I’ve decided to dedicate a series of posts to Rails Best Practices because I think it is important to share in order to continuously improve the way you write programs.Write these posts will also be an opportunity for me to investigate my knowledge, so I invite you right now to follow this trip with me and contribute with your comments.This first post is just an...

Read more

Partial Validation in Rails

It happens sometimes that you have to implement creation or editing of a model splitted on more than one form. A classic case is a registration form divided into two steps or a guided wizard with several pages.This situation presents us with a problem: when and how to validate data?The first idea that may come to mind is simply to leave it all validations in the last page when you go t...

Read more