📄 Table of Contents

RUBY ON RAILS

Ruby is to Rails as PHP is to Symfony and Zend, or as Python is to Django. The appeal of Ruby to developers lies in the elegance and terseness of the language.

Why ROR

From project management point of view, the Ruby on Rails community advocate Agile web development — an iterative development method, that encourages collaborative and flexible approach, which is particularly well-suited for web application development with fast-changing requirements.

Model is a database + for handling data and business logic

View is your visual aspects : the html + css, etc. + for handling graphical user interface objects and presentation

Controller helps the model and the view talk to each other.

This separation results in user requests being processed as follows:

  1. The browser sends a request for a page to the controller on the server
  2. The controller retrieves the data it needs from the model in order to respond to the request
  3. The controller gives the retrieved data to the view
  4. The view is rendered and sent back to the client for the browser to display

POTENTIAL RAILS PROBLEMS AND LIMITATIONS AND HOW TO OVERCOME THEM:

There is also the option of running your application under JRuby, so you have the same performance characteristics as Java.

ROR Commands

So, to recap, keep these 5 helpful commands handy when your building your next MVC app:

  1. $gem install rails → installs rails

2. $rails new <project-name> → creates a new project

3. $rails generate <controller-name> <view-name>

creates a controller (make sure it’s plural) + a corresponding .html.erb view file as a bonus + layout .

For example if we have a controller like this:

class SamplesController < ApplicationController
def show
render ‘show’ #this can be omitted
end
end

show is a method of SamplesController class. If no action is defined then it simply renders the view along with layout. We should have a a view file inside app/views/samples called show.html.erb. Also, if we don’t have any layout, simply put layout false.

4. $rails generate model name [field[:type] field[:type]

creates our model and also specify what we’d like to store in our schema. With the $rails generate model command, we can name our model and also specify what we’d like to store in our schema.

But why go through the trouble of manually creating your own models and controllers when you can generate all of this and more with $rails generate scaffold?

5. $rails generate scaffold

With Rails generate scaffold, we can generate our model, a database migration for the model, and a controller with actions and views! All done with one command.

Scaffolding is like a shortcut. Scaffolding automatically generates the models, views and controllers you need for a table. A quick google search tells us:

Scaffolding in Ruby on Rails refers to the auto generation of a simple set of a model, views and controller usually for a single table. Would create a full CRUD (create, read, update, delete) web interface for the Users table”

ROR syntax

def index
render(“hello”)
redirect_to(controller => “demo”, action => “index”)
redirect_to(“http://youtube.com")
end
<%= 5 + 5 %>
<% var = “rubyOnRails” %>
<%= “hello” #{ var } %>
@instance_var =[1, 2, 3, 4, 5]// how to link to other pages
< a href = “/demo/hello” > Hello page </a >
<%= link_to(“page to link to”, {action => “hello”})

VIEWS

A view need not actually contain any Ruby code at all — it may be the case that one of your views is a simple HTML file; however, it’s more likely that your views will contain a combination of HTML and Ruby code, making the page more dynamic. The Ruby code is embedded in HTML using embedded Ruby syntax.

Styling in ROR

There are 3 basic categories of styling that we can use to manipulate the html in our apps and make it more presentable. These categories include inline, internal and external styling.

It is advised that we never use this first 2 forms of styling when coding.

<h1 style=”color:blue”>Hello Rubyers!</h1><style type=”text/css”> 
h1{color: blue;}
</style>

[3]. External Styling

The third category of styling would fall under external styling which is when a separate css file is created and all of the styling syntax is placed in that separate file.

When creating a rails application we are welcomed by a not so blank canvas that will be used for code. In the sea of different files created we can follow the path app/assets/stylesheets and create a new file style.css under the stylesheets folder. This file can be called anything but make sure you end it with the .css extension.

Now that we have a .css file we can move to app/views/layouts and open the application.html.erb file and enter a stylesheet link tag that will connect the applications html file with the stylesheet. The string should be the name of your stylesheet.

<%= stylesheet_link_tag 'style.css' %>

With this done we are ready to begin to style and make simple plain text pages into something completely different. Remember when adding your styling logic always add a semicolon<declaration separator> at the end of each declaration.

What is @/@@?

There are 4 types of variables in Ruby: local , global, instance, and class. The @ symbol before a variable tells Ruby that we are working with an instance variable, and @@ before a variable tells us we are working with a class variable. We use @ before a variable in instance methods within a class to tell Ruby to access that attribute (instance variable) of the instance. Using @instance_var makes instance_var accessible to all instance methods. Similarly, we use @@ before a variable in class methods to tell Ruby to access that attribute (class variable) of the class. Using @@class_var makes class_var accessible to the entire class. When we use @instance_var or @@class_var, we are accessing that instance or class variable directly.

Here is an example:

What is self?

You may have noticed the word self in the definition our class methods. Every object in Ruby has and is aware of its self. The keyword self in Ruby enables you to access to the current object — the object that is receiving the current message.

Setup Redis on Mac

type below:

brew update
brew install redis

To have launchd start redis now and restart at login:

brew services start redis

to stop it, just run:

brew services stop redis

Test if Redis server is running.

redis-cli ping

If it replies “PONG”, then it’s good to go!

Or, if you don’t want/need a background service you can just run:

redis-server /usr/local/etc/redis.conf

Location of Redis configuration file.

/usr/local/etc/redis.conf

Uninstall Redis and its files.

brew uninstall redis
rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

Install Elasticsearch on macOS with Homebrew

Elastic publishes Homebrew formulae so you can install Elasticsearch with the Homebrew package manager.

To install with Homebrew, you first need to tap the Elastic Homebrew repository:

brew tap elastic/tap

Once you’ve tapped the Elastic Homebrew repo, you can use brew install to install the default distribution of Elasticsearch:

brew install elastic/tap/elasticsearch-full

This installs the most recently released default distribution of Elasticsearch. To install the OSS distribution, specify elastic/tap/elasticsearch-oss.

https://selleo.com/til/posts/esrgfyxjee-how-to-fix-elasticsearch-forbidden12index-read-only

Install/Check Mongodb on macOS

To check if mongoose module is installed simply find the version by

npm list mongoose

To check globally

npm list -g  mongoose

If you are using MongoLab, to connect to database type: mongo

If you are running Mongo locally, start the Mongo daemon:

In the terminal type: mongod
If that doesnt run try: sudo mongod
Once mongoDB is running open another terminal window and type: mongo
In the same window type: show dbs
To work with a database in the list named myDB, for example type : use myDB

Initially released in 2010, Elasticsearch (sometimes dubbed ES) is a modern search and analytics engine which is based on Apache Lucene. Completely open source and built with Java, Elasticsearch is a NoSQL database. That means it stores data in an unstructured way and that you cannot use SQL to query it

Ruby on Rails — HTML.ERB

HTML.ERB is HTML mixed with Ruby, using HTML tags. All of Ruby is available for programming along with HTML.

Following is the syntax of using Ruby with HTML −

<% %>   # executes the Ruby code
<%= %> # executes the Ruby code and displays the result

Example

<ul>
<% @products.each do |p| %>
<li> <%= @p.name %> </li>
<% end %>
</ul>

The output of anything in <%= %> tags is directly copied to the HTML output stream. To secure against HTML injection, use the h() function to html_escape the output

For example

<%=h @user_entered_notes %>

FINALLY, LET’S MERGE IN THE HTML AND ADD SOME ERB

The first thing you want to do is open up your “app/views/layouts/application.html.erb” file and merge in the markup from Tim’s “homepage.html” file that is going to be on all our pages:

render & render partial:

render partial: 'some/path/to/my/partial', locals: { custom_var: 'Hello' }

yield & content_for

A small example about yield:

In your layout:

<html>
<head>
<%= yield :html_head %>
</head>
<body>
<div id="sidebar">
<%= yield :sidebar %>
</div>
</body>

In one of your view:

<% content_for :sidebar do %>
This content will show up in the sidebar section
<% end %>
<% content_for :html_head do %>
<script type="text/javascript">
console.log("Hello World!");
</script>
<% end %>

This will produce the following HTML:

<html>
<head>
<script type="text/javascript">
console.log("Hello World!");
</script>

</head>
<body>
<div id="sidebar">
This content will show up in the sidebar section
</div>
</body>

How do I auto format Ruby or .erb files in VS Code?

You can set format associations in VSCode, so .erb files would be treated like .html.

Go to File->Preferences->Settings->Click … in top right corner->Open settings.json

Then add this piece of code to your settings.json

"files.associations": {
"*.html.erb": "html"
}

This is how I solved this problem. It will remove some of the code highlights but will autoformat HTML templates like an HTML document.

Use CSS to automatically add ‘required field’ asterisk to form inputs

<div class="required">
<label>Name:</label>
<input type="text">
</div>
<style>
.required:after { content:" *"; }
</style>

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store