Ruby On Rails : Tools, Tips & Tricks to be more efficient developer

As a developer there are some techniques to be more efficient at work.

Guake terminal :

We can use OS’s terminal but it would be more efficient if we use Guake terminal. Normal configuration of Guake terminal doesnot load itself at startup. Tips reagarding Guake  terminal

> Put guake terminal at start up list in Ubuntu

– Open ‘Startup Applications’ > Click ‘Add’ > Write { Name: ‘Guake terminal’, command: ‘guake -n /path/of/your/interest/’} > click ‘Add’ > You are done!

if you want a new tab to execute a command you wish and even change the label then

guake -n ~/projects/ATS/ -r "SideKiq" -e "bundle exec sidekiq -q default -q mailer"

for more details see the following link

Change Shortcut keys

By default, the shortcut key to toggle the terminal is ‘F12’. However, we can change the shortcut key. F12 key resides on right hand side of the keyboard. If you use mouse then, you might feel uneasy to move the hand just to press F12 key. You can change the shortcut key to ‘Fn’ + Q. It depends on your preference.

Open new tab in current directory

There is a check-box option called ‘Open new tab in current directory’ in the ‘Preference’ dialog box. It means if you have changed you directory to ‘~/Download/my_app/’ and open a new tab then you can see the same path in the prompt.

Difference between Ruby’s Load, Include, Require and Extend

When you Include a module into your class as shown below, it’s as if you took the code defined within the module and inserted it within the class, where you ‘include’ it. It allows the ‘mixin’ behavior. It’s used to DRY up your code to avoid duplication, for instance, if there were multiple classes that would need the same code within the module.

The following assumes that the module Log and class TestClass are defined in the same .rb file. If they were in separate files, then ‘load’ or ‘require’ must be used to let the class know about the module you’ve defined.

module Log
  def class_type
    "This class is of type: #{self.class}"
  end
end
class TestClass
  include Log
  # ...
end
tc = TestClass.new.class_type
puts tc

The above will print “This class is of type: TestClass”

The load method is almost like the require method except it doesn’t keep track of whether or not that library has been loaded. So it’s possible to load a library multiple times and also when using the load method you must specify the “.rb” extension of the library file name.

Most of the time, you’ll want to use require instead of load but load is there if you want a library to be loaded each time load is called. For example, if your module changes its state frequently, you may want to use load to pick up those changes within classes loaded from.

Here’s an example of how to use load. Place the load method at the very top of your “.rb” file. Also the load method takes a path to the file as an argument:

load 'test_library.rb'

So for example, if the module is defined in a separate .rb file than it’s used, then you can use the

File: log.rb

module Log
  def class_type
    "This class is of type: #{self.class}"
  end
end

File: test.rb

load 'log.rb'
class TestClass
  include Log
  # ...
end

The require method allows you to load a library and prevents it from being loaded more than once. The require method will return ‘false’ if you try to load the same library after the first time. The require method only needs to be used if library you are loading is defined in a separate file, which is usually the case.

So it keeps track of whether that library was already loaded or not. You also don’t need to specify the “.rb” extension of the library file name.

Here’s an example of how to use require. Place the require method at the very top of your “.rb” file:

require 'test_library'

When using the extend method instead of include, you are adding the module’s methods as class methods instead of as instance methods.

Here is an example of how to use the extend method:

module Log
  def class_type
    "This class is of type: #{self.class}"
  end
end
class TestClass
  extend Log
  # ...
end
tc = TestClass.class_type
puts tc

The above will print “This class is of type: TestClass”

When using extend instead of include within the class, if you try to instantiate TestClass and call method class_type on it, as you did in the Include example above, you’ll get a NoMethodError. So, again, the module’s methods become available as class methods.

Rails : gem ‘rails4_client_side_validations’ to validate the forms in popup

How the gem works?

For the form you want to validate, you maintain the validations in the respective Model. The validation set in the model works for server-side validation. For client-side validation this gem actually creates the required js codes the validation you set in the model.

Solution No: 1

# Just after the form is loaded in the DOM if your gem is client_side_validations
$(new_form).enableClientSideValidations();

# Specifically for rails4_client_side_validations gem
$(new_form).enableRails4ClientSideValidations();

Solution No: 2

So, in case of popups the required js codes have not been loaded in the document yet, so you need to render the js too with the view for the popup

In my case I created a layout specific for this popup or all this type of popups. and just rendered the validator’s js

Rails : Layout and rendering :: Gist/Theme

2 Creating Responses

From the controller’s point of view, there are three ways to create an HTTP response:

  • Call render to create a full response to send back to the browser
  • Call redirect_to to send an HTTP redirect status code to the browser
  • Call head to create a response consisting solely of HTTP headers to send back to the browser

    2.1 Rendering by Default: Convention Over Configuration in Action

    if you have this code in your BooksController class:

    class BooksController < ApplicationController
    end

    And the following in your routes file:

    resources :books

    And you have a view file app/views/books/index.html.erb:

    <h1>Books are coming soon!</h1>

    Rails will automatically render app/views/books/index.html.erb when you navigate to /books and you will see “Books are coming soon!” on your screen.

  • The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the action_name.html.erb template in the controller’s view path and render it. So in this case, Rails will render the app/views/books/index.html.erb file
    2.2.1 Rendering Nothing

    Perhaps the simplest thing you can do with render is to render nothing at all:

    render nothing: true
    or
    head :bad_request
    2.2.2 Rendering an Action’s View

    If you want to render the view that corresponds to a different template within the same controller, you can use render with the name of the view:

    def update
      @book = Book.find(params[:id])
      if @book.update(book_params)
        redirect_to(@book)
      else
        render "edit"
       # render :edit you can rather use symbol
      end
    end

    If the call to update fails, calling the update action in this controller will render the edit.html.erbtemplate belonging to the same controller.

    2.2.3 Rendering an Action’s Template from Another Controller
    you can render the results of an action to a template in app/views/products this way:
    render "products/show"

    you can use the :template option (which was required on Rails 2.2 and earlier):

    render template: "products/show"
    2.2.4 Rendering an Arbitrary File
    render "/u/apps/warehouse_app/current/app/views/products/show"
    or
    render file: "/u/apps/warehouse_app/current/app/views/products/show"
    2.2.7 Rendering Text

    You can send plain text – with no markup at all – back to the browser by using the :plain option to render:

    render plain: "OK"

    Rendering pure text is most useful when you’re responding to Ajax or web service requests that are expecting something other than proper HTML.

    2.2.9 Rendering JSON

    JSON is a JavaScript data format used by many Ajax libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:

    render json: @product
    2.2.10 Rendering XML

    Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:

    render xml: @product
    2.2.11 Rendering Vanilla JavaScript

    Rails can render vanilla JavaScript:

    render js: "alert('Hello Rails');"

    This will send the supplied string to the browser with a MIME type of text/javascript.

    2.2.13 Options for render

    Calls to the render method generally accept four options:

    • :content_type
    • :layout
    • :location
    • :status

2.2.13.1 The :content_type Option

render file: filename, content_type: "application/rss"
By default, Rails will serve the results of a rendering operation with the MIME content-type of text/html (or application/json if you use the :json option)
2.2.13.2 The :layout Option
You can use the :layout option to tell Rails to use a specific file as the layout for the current action:
render layout: "special_layout"

You can also tell Rails to render with no layout at all:

render layout: false
2.2.13.3 The :location Option

You can use the :location option to set the HTTP Location header:

render xml: photo, location: photo_url(photo)
2.2.13.4 The :status Option

Rails will automatically generate a response with the correct HTTP status code (in most cases, this is 200 OK). You can use the :status option to change this:

render status: 500
render status: :forbidden

To include app/assets/stylesheets/main.css and app/assets/stylesheets/columns.css:

<%= stylesheet_link_tag "main", "columns" %>

To include app/assets/stylesheets/main.css and app/assets/stylesheets/photos/columns.css:

<%= stylesheet_link_tag "main", "photos/columns" %>

To include http://example.com/main.css:

<%= stylesheet_link_tag "http://example.com/main.css" %>