Featured
-
Installing RVM and Creation of Gemsets Part 2
In the last tutorial we learnt how to install RVM.
by dinesh.singh -
Installing RVM and Creating of Gemsets Part 1
Installing RVM and creating Gemsets can be difficu
by dinesh.singh -
Use pik in Windows
Managing multiple versions of Ruby on Windows can
by anirudh.rautela -
Using Bundler in Rails 2.3.x app
Managing Gems in Rails can be infuriating at times
by anirudh.rautela
Tags
Understand Rails Authenticity Token
What happens when the user views a form in the browser for a resource, rails application creates a random string as a authenticity token and store that random token in the session and when the form generates, it places that token in the form as a...
What are Rake tasks ?
Rake is a tool that is written in Ruby language and very similar to Ant, Make. The difference between the other tools and rake is that it is a domain specific language which means outside the boundaries of rails it has no existence. It basically ...
Before and After Filters in Rails
Filter in Rails
Filters are the methods, that run before, after or around a controller action. We can better understand it by taking an example. Suppose in a blogging site, there are groups and we want that a user can only read blogs of his gr...
Error installing mysql2: Failed to build gem native extension
This error used to come when you are trying to do bundle install and mysql2 gem is unable to install properly. So below are the steps as per the different operating systems:
Sometimes you need to update your Ruby library, run this code:
Ste...
Invoking controller/view methods from the console in Rails
At the time of development of a module or functionality we always curious that the code we wrote will work perfectly or not until and unless we run that in a browser. so rails provide us a way where we can test the functions via a console. By run...
Difference between attr_accessor and attr_accessible
The main difference between attr_accessible and attr_accessor is that attr_accessible is a Rails method that basically specifies a white list of model attributes that can be set via mass-assignment and on the other hand attr_accessor is a ruby me...
Rails: Using forms for operations like Patch, Put , Delete
Hi friends,
As we all know, rails always encourages RESTful (REpresentational State Transfer) design for resources, that specifies standards for using different kind of requests. Rails also supports this. Thus if you are using RESTful, you would...
Date and Time Helpers in Rails
Date and Time Helpers in Rails
Date and time selection are very common in forms for saving event times, schedules, birth date etc. Rails also provides date and time helpers for taking inputs of date, time or datetime. While dealing with date...
Uploading Files in Rails
Uploading Files in Rails
Uploading files from a form is a very common task(i.e uploading images, files etc). As you may already know that you need to set multipart/form-data, if you want to upload files from a form. Similarly in rails, you need ...
Creating Select Boxes using form helpers in rails
One of the most common html elements in a web page is select box. In Rails select and option tag helper method is used to display it .
select and option tag:
<%= select_tag(:city_id, options_for_select[[Bangalore, 1], [Dehradun, 2]]) %&g...
Rails:Populating Select boxes with model data
In model specific form we use select method to display a select box and populate it with model data.
Example:
#controller
@location = Location.new()
#view
<%= select(:location,:city_id,City.all.collect {|p| [ p.name, p.id ] }) %&g...
Creating Basic Search Form in Rails
Hi Friends,
As we know that, currently there are plenty of content based sites and we usually see a search box within that, to search any topic that is available in that site. So today we will be discussing on how to create a basic search form i...
Form Helpers in Rails Part-4 (Building Complex Forms)
Hi friends,
In my previous blog, we talked about customization of form helpers and how to create forms for external resources through form helpers in Form Helpers in Rails Part-3. Now we will switch to discuss on how to build complex forms in ra...
using head to create header response
While sending an ajax request in rails sometimes we dont need to render any data back to the page . Only status 200 is sufficient to show changes in the view page then we use render :nothing option but there is an alternative to it is head metho...
Form Helpers in Rails Part-3 (Customization and Forms for External Resources)
Hi friends,
Previously we talked about form helpers dealing with models in Form Helpers in Rails Part-2. Today we will continue with Form Builders Customizations.
As we already discussed that objects from form_for and fields_for are instance of...
Use of Render option in Ruby on Rails
render() method in rails is used for rendering a content to the view . Its uses are:
In Controller for rendering an action
Example:
render :action => "show"
This is the most common use of render in controller . The template for sh...
Form Helpers in Rails Part-2 (Model Objects)
Hi Friends,
In my previous blog Form Helpers in Rails Part-1, we have talked about only the basic form helpers. Today we will be discussing forms with models. As we know in MVC, forms are mostly used for creating, updating or editing models. So...
Form Helpers in Rails Part 1 (Basics)
Hi Friends,
In my previous blogs, I have talked about so many topics related to Ruby on Rails. Now today I am going to start form helpers in rails. As all the points can't be covered in one blog, I am dividing it into parts. Today I will be deal...
Applying partials to layouts in rails
In Rails we can use partials to organize our code in the layouts . This is generally helpful when we have layouts running up to hundreds of lines. For example we have a top nav bar which looks different for logged in and non logged in user. Then ...
Use of :as in Passing parameters to rails partials
While passing objects to partial we can change the name of the local variable in the partial and for that purpose we need to use the :as option.
For example we have a partial _account.html.erb where we need to pass a collection @cutomers a...
Capturing part of template in variable in RoR
Hi Friends,
Sometimes you need to use a small part of a template at so many places and you have to write the same lines of code on separate places and you also don't want to create separate partials for that. Rails gives a solution to your probl...
Number Formatting in RoR Views
Number Formatting in Rails
Numbers are of different formats, according to their uses like phone numbers, percentage, currency etc. In Rails NumberHelper fulfills this requirement by providing methods for formatting numbers into different forms. ...
Passing parameters to rails partials
While rendering a partial if we want to send parameters to the partial we can do it like this:
<%= render :partial => user_profile_list, :locals => { :user => @user } %>
or
<%= render :partial => "user_profile_list", :...
Benchmark helper in rails
Rails has benchmark helper to quickly test the time of execution of a given piece of code. Active Record, Action Controller and Action View libraries provides a benchmark() method to measure the performance of code.
Example:
Model(Active Rec...
Atom Feeds in Rails
Hi friends,
We have recently talked about auto_discovery_link_tag, that consumes RSS and Atom feeds. Now we are going into deep what is the atom feeds in Rails and how we can achieve that. Lets begin with a defining Atom Feeds.
Atom Feeds are ...
auto_discovery_link_tag in rails
Hi guys,
Today we are going to discuss about auto_discovery_link_tag in rails. It comes under AssetTagHelper, which gives methods for generating HTML that links views to assets(i.e images, JavaScripts, stylesheets, and feeds).
For consuming RS...
Builder templates in ROR and its uses
Action view templates can be written in various ways depending on the format of the response. If the response is in html format then we can use .erb files i.e. html files, with embedded ruby in it.
If the format is in xml then we need to use fil...
Introspection using ActiveModel
ActiveModel is a library which contains various modules which when added to a class add some features present on Active Record to the class. One such module is ActiveModel::Naming module. This module is used for model name introspection .Below ...
Dirty methods in Rails model
Sometimes we face a situation where after changing the value of an attribute of an object we need the previous or the unchanged value for some purpose. We generally store the previous value in a variable to be used later. Instead of this approach...
Rails Model Translation
Rails Model Translation
Translation in Rails active records is used to provide integration between object and the Rails internationalization (i18n) framework. Rails does this using ActiveModel::Translation.Example:
class Blog
ext...
Rails model Serialization
Rails model Serialization
Hi Friends,
Earlier we have discussed about Association in Rails Model. Today we will be covering one more topic related to rails active records, that is Serialization. In summary we can define serialized objects as, S...
Join in rails model
In rails if we want to establish inner join relationship between two models we can do it using joins method of activerecord.
For example, consider the following Company, Product, Review and Vote models:
class Company < ActiveRecord::Base...
Associations in Rails Model
Associations in Rails
Associations are an important part of each Active Records. It makes coding easier and relations clean. Lets take an example of Blog. A Blog can have many comments. So if you delete blog its associated comments also needs ...
Reorder in Rails
Hi friends,
Today we are going to discuss about reorder in rails. This method is used to override the scope of order and reorder the rows according to the passed arguments.
Example:
Blog.order('title DESC').reorder('published_at') # generate...
Having Clause in Rails
Hi Friends,
Previously we discussed about Group By Clause in Rails, Now today lets discuss a related clause of SQL, which is Having. SQL uses having for specifying conditions on GROUP BY clause. Suppose we want to fetch the total views of blogs ...
GROUP BY clause in Rails
Hi Friends,
As you know GROUP BY is used to group the result-set by one or more columns. The SQL query to fetch the total views of blogs created at same day will be:
SELECT date(created_at) as creation_date, sum(view) as total_views
FROM blo...
Self Join in Rails
In Self join association one model has relation with itself. For example one user table can have list of all coach and player. One coach can train many players. We will represent the association as:
class User < ActiveRecord::Base
has_m...
Fetching records in batches in rails
Hi Friends,
Many times we get into a situation where we have to fetch a large record from a table and need to iterate over it and that makes our server cry. As it consumes a lots a memory and time. So no need to worry again for that Rails has a...
Polymorphic association
Polymorphic association is little tricky to understand among all the rails association. Using this association one model can have belongs_to association with more than one model in a single line of association. As for example in a social networki...
Fetching Records in Rails using take
take in Rails:Rails has so many methods that provide the facility to fetching the records from database, so that you don't need to write raw sql. One of them is take.
By using take we can fetch a record (or N number of records if specified as a ...
find_by in Rails
Hi Friends,
Rails provides several methods for fetching single object from table, one of the method is find_by. It fetches the first record in the table matching some specific conditions. Example can be if we want to fetch the first blog with ti...
Choosing Between has_many :through and has_and_belongs_to_many
In Rails we can establish many to many relationship between two models in two ways 1) by using has_and_belongs_to_many 2) by using has_many :through associations.
We generally use HABTM when we dont require a third intervening model to join t...
Unique Validators in Rails
Hi Friends,
Today we will be going to discuss a very basic constraint of rails validation that is uniqueness, as its name suggests, Unique Validator is a rails validation helper used to check an attribute value in database and if it already exis...
Strict Validator in Rails Model
Hi friends,
We already talked about different kinds of validations. Today lets discuss one of the common type of validation that is Strict validation. By using this we can check whether an object is valid or not and can raise ActiveModel::Strict...
Numeric Validator in Rails
Hi Friends,
Thanks for viewing my previous blogs, hope you liked them. As we were previously talking about validations starting with Format validations in Rails, lets continue with that with a new validation helper numericality. As its name rep...
Length Validators in Rails
Hi Guys,
In my previous blog Format validators in rails, I talked about validations in rails and now I am here again with one of the most commonly used helper available in rails, that is length.
Length validator is used to validate the length ...
Format Validators in Rails
Hi Friends,
Hope you are doing well with your work. Today I am here with one of the most common topics in Rails that is format validators in rails. Lets start with defining validation.
"Validations are used to avoid the invalid data to get sto...
Choosing Between belongs_to and has_one
If we have has_one relation between two models then we need to declare belongs_to relation in one model and has_one relation in another model. belongs_to will be declared in the model where we will have the foreign_key and has_one in the other mo...
Virtual attributes in Rails like acceptance and confirmation
Hi Friends,
We have talked about few Rails topics earlier like validates associated and overriding naming convention of table names. Here I am again with one more topic Virtual Attributes in Rails. Before discussing that lets look into a situati...
Pessimistic locking in ruby on rails
In pessimistic locking a particular row in the database is exclusively locked so that other updates on the same record are blocked . This mechanism has the advantage that when a certain operation is going on the selected row other user cannot rea...