Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 

how to do ajax call through jquery function in rails 4 ?

This is our ruby on rails  view from where called our jquery function().  <td id= <%= "reject_#{event.id}" %>> <%= button_tag "Accept" , :onclick => "abc('#{event.id}', 'Accept');" %> ...

class_eval and instance_eval

What is the use of class_eval and instance_eval? class_eval and instance_eval allows us to define a method of a class from outside of its original definition and without reopening the class with the standard syntax. This could be useful when...

Difference between include & extend in Ruby Classes

Ruby provides us the facility of Modules. Modules is to ruby as package is to java and namespace is to c#. We use a module to keep similar kind of classes together, and promote code Re-usability, i.e If a set of code is to be used in multiple ...

Background job using whenever gem rails

Whenever gem is used to easily schedule tasks using cron jobs, it enables you to manipulate standard functionality of your machine using Ruby code. 1: To start with Whenever we have to install gem gem install whenever # insert this li...

difference between HAS_ONE and BELONGS_TO association in rails?

Has_one and Belongs_to relationship sometime become confusing so the main difference can be explained with the help of example: 1>Has_one :- class Department < ActiveRecord::Base has_one: employee end this means that employee tab...

Difference between first & take in Ruby Arrays

Both first & take are applied on the ruby objects, they return the requested number of elements from the Array. There are some differences between them, and i have explained the major ones over here. 1) The method take always accepts an arg...

Difference between && and and in ruby.

Ruby supports both && as well as and. The basic difference between these 2 lies in their precedence order. && has higher preference than and. and has lower preference than = unlike && which has higher preferen...

Constructors in Ruby

Ruby also supports constructors like other Object Oriented Languages. Constructors are used to initialize the instance variables. In most of the Object Oriented Languages constructors has the same name as that of the class, and does not have any ...

How to extract today's date & time in Rails

If you want to extract only the date, we can use the strftime method of Time/Date class, as 2.1.5 :055 > DateTime.now.strftime('%d/%m/%Y') => "12/30/2015" (Here d is used to display date, m displays month and Y display the ye...

How to send a Hash as an argument to a function in Ruby

In Ruby it is possible to send a Hash as an argument to a method. Suppose we have a method named conversion_method which has 4 default parameters(a,b,c,d). ex:- 2.1.5 :086 > def conversion_method(a:10, b:20, c:30, d:40) 2.1.5 :087?&g...

How to print an object's class & contents in Rails

If i have a User object say user, i.e >> user = User.find(1) => #<User id: 1, login: "james.warner", email: "james.warner@evontech.com",created_at: "2013-11-18 07:12:45", updated_at: "2014-02-03 10:51:02", designation: "Programmer...

Split array up into n-groups of m size?

A very good method i found in ruby so just thought to share it with everyone, so i am writing this blog to share and help others. This particular blog is for splitting up of array into many groups. Lets see this as an example below: 1) arr = (...

Use .pluck If You Only Need a Subset of Model Attributes

If you want few attributes from a table, instead of instantiating a collection of models and then running a .map over them to get the data you need, its very much economical to use .pluck to pull only the attributes you need as a array. There...

Spliting resultset over array

I remember i was working on a project where there was a large set of data in the resultset and on the basis of that data i have to update some other model but i was unable to do it in batches due to some reason. So i was searching for a method in...

Sort an array of strings based on length of its elements in Ruby

In ruby if you want to sort an array of strings based on the length of its element, you can use the code given below. i.e If you want the word having the least number of characters in it to come first and arrange other words in this manner as w...

Remove Leading, Trailing and other consecutive white spaces in a string in RoR

There are innumerous ways to remove extra white spaces from a string in Ruby on Rails. Here i am demonstrating 3 ways which you can use to achieve the desired result. 1> squish function : This function was introduced in Rails framewor...

Different ways to invoke method in ruby

Ruby provide with the following ways by which you can invoke the method based on your requirement : s= "ruby method" 1) Call a public instance method directly p s.length #=> 11 2) Other way is to invoke a method dynamically in ...

SETTING UP ROR FOR THE FIRST TIME ON A NEW UBUNTU SYSTEM USING RVM

1> We need to install RVM before we can install Ruby, because RVM is a version manager and it will help us to install & manage different versions of Ruby on the same system and easily switch between them. To install RVM we first need to...

each, collect and map in ruby

1> 'each' will loop through each element of the array and evaluate whatever is return inside the block, but it returns the original array without any change. a = [5,10,15,20] => [5, 10, 15, 20] a.each {|t| t+2} => [5, 10, 15, 20...

Using reject in ruby to delete elements selectively from an array

1> To delete an element from an array, use reject as shown below: arr = ['a','b','c','d','e'] arr.reject! { |i| i.match('d')} Result :=> ["a", "b", "c", "e"] 2> To delete particular keys from the array whose elements are ...

Single Table Inheritance in Rails

STI can be considered where single table can be used to represent multiple models based on different type. The parent model inherits from ActiveRecord::Base. Other models inherit from parent model. Data store and accessed from same table for all ...

How to get date with suffix st,th etc

While getting date in rails we do have a requirement where we need to show suffix attached to date like 2nd,3rd etc. There is no direct rails date function to get the same so in order to get date in the following format Thu, Nov 5th 2015 ...

Using rescue in single line

We can use rescue in single line to return a value if code of that line raise an error/exception, example shown below-: Let say we have a hash: person = {:name => "ABC"} person[:name].downcase # "abc" ...

Create classes in ruby

Ruby is a pure object oriented language and everything in ruby is an object of some class. In ruby we can create classes and create objects of the classes. In the below example we will create 3 classes Person, Address and Company. An object of...

Applying limit and offset to array

In rails we can apply limit and offset to arrays in different ways. 1> Using "slice" method. slice takes 2 arguments, the first one being the "starting_index" & the second one is the "number_of_elements". syntax :- arrayob...

How can one install ruby on ubuntu

Choose the version of ruby you want to install for example : 2.2.3 After choosing the right version of ruby first step is to install some dependencies : sudo apt-get update sudo apt-get install git-core curl zlib1g-dev build-essential libs...

Strong Parameters in Rails

Strong Parameters in Rails Strong parameters are used to prevent the action controller parameters that are used in Active Model Mass Assignment. If you want to use them, you need to white-list them. It is basically used as a security for sensiti...

default_url_options in Rails

default_url_options in Rails Rails has a url_for helper, which is used for generating hyperlinks. So many times we want that a certain thing or parameter needs to be passed with each link or request to tell the controller to do certain thing...

Use of Flash in Rails

Use of Flash in Rails In rails, flash is also used as a session management method. Its feature is that it gets cleared after every request. It is basically used when we just want to show a message in the next request or view and don't want t...

JQuery Ajax Post in Rails with example

JQuery Ajax Post in Rails As we all know, ajax is used for saving some objects to server or requesting to server and in response, page should not be refreshed. Rails provides helper remote: true, that can be added in ruby form helpers. After a...

Using Cookies in ROR

Cookies are objects that we use to store some information of the user in the browser like geolocation of user . It is a set of key value pair. All cookies have time of expiry after which they are just deleted from the browser usually at the end o...

Rendering XML and JSON in rails

Rendering xml or json data from the controller method is very easy. We have to just follow this syntax. Example(xml): def index @users = User.all respond_to do |format| format.xml {render :xml => @users} ...

Single Table Inheritance And where to use it in Rails

This particular post will give you a overview of Single Table Inheritance And where to use it in Rails. Single Table Inheritance is, as the name suggested, it is a way to add inheritance to your models. STI lets you to save different models i...

Adding Translations in Rails

Adding Translation to a Rails Application Hi friends, in my previous blogs related to internationalization of rails application, we have configured the i18N api and saw different types of methods to pass the locals, if you have not read thos...

Setting Rails Locales from URL

Rails Internationalization : Setting locals from URL In my previous blog, we looked in Setting rails locale from domain name, it was a good approach of detecting the desired locale from domain or subdomain, but sometimes you are not able to ...

Setting rails locals from Client Requests

Rails Internationalization : Setting locals from Client Requests Hi friends, in my previous blog, we looked into Setting the Locale from the URL Params. There are some more cases where we don't want to set the locales through URLs or domains...

Setting rails locale from domain name in rails for internationalization

Rails Internationalization : Setting locale from Domain Name For translating your application to other language, you can set I18n.default_locale to your locale in application.rb or in initializers as I told in my previous blog Configuring i1...

raw vs. html_safe vs. h to unescape html

Rails has a nice way to protect yourself from CSRF attacks through threatening HTML encoding from anything you write between <%= %>. But there is a caution, you want to render HTML from string so you need to tell rails that it should not es...

Is Ruby pass by reference or by value?

When we have to call a function with parameters there are basically two most generally known and easy to understand approaches for passing of parameters among all programming languages and these are pass-by-value and pass-by-reference. Ruby is pa...

Using session objects in Ruby on Rails

Session objects are used in ROR to store small amount of information which can be used later on. In fact websites following http (a stateless protocol) use session object to keep logged in user informations so that those informations persists b...

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...

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...
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: