ruby-on-rails

Rails briefly

Follow me
Follow me

Latest posts by Tomasz Antas (see all)

Jan 26, 2015

The brief descriptions about using the basic Rails’ components. This article contains mainly the pure commands without a detailed description.

 

  1. Creating new project in Ruby on Rails

  2. MySQL usage in Rails app

  3. Bootstrap

  4. Partials in view – bootstrap navigation bar

  5. Models relations

 

1. Creating new project in Ruby on Rails

 

Create new application (named tasker in this example):

If you want use another database than SQLite, use one of following command instead of previous one:

We prefer to use MySQL database.

 

RESOURCES

In config/routes.rb add resources (in our example the resource is called task):

And execute:

It should set up a necessary routing for task resource.

 

MODEL

Generate Task model with needed attributes:

Allowed types:

:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean, :references

It’s possible to set additional features in “rails generate model” command, like precision and scale for decimal type field:

$ rails generate model Place name:string latitude:decimal{10.6} longitude:decimal{10.6}

And run:

It can be needed to define the user and password to database before you’ll try execute previous command. If you’ve executed the rake db:migrate and you get message “Access denied…“, probably it means that you didn’t set up user and password. To do fix it, open config/database.yml and set proper username and password in proper environment sections. For example:

 

 CONTROLLER

Create controller for task:

Below we placed the basic template for controller and set of needed views:

Controller is placed in app/controllers and is named tasks_controller.rb. It is necessary to create a views as separate files for each action (excepts delete action). Views are placed in app/views/tasks.

 

 

2. MySQL usage in Rails app

Creating Rails project with MySQL database:

ADDING NEW USERS AND SETTING  PRIVILEGES

Login to mysql shell in terminal:

Enter the password (sometimes password can be ‘root’ or it’s not set yet, so leave it empty in second case).

Create new user:

List all databases:

Give a ALL PRIVILIGES to proper database:

Give a ALL PRIVILIGES to all databases:

Logout from mysql:

To check that everything is fine, try login to mysql as new user:

To view grants, use following command:

 

USE NEW USER ACCOUNT IN RAILS APP

Change username and password in config/database.yml:

To make it more safely, you can keep a database username and password secret. Open config/secrets.yml and add there database username and password:

Add secrets.yml to .gitignore:

Finally, modify the database.yml file:

 

Alternatively, you can export password as environment variable. You can store it in different place, like in /etc/profile/ or ~/.bashrc files. We can add it also to /etc/environment:

In next step, you have to restart session (login and logout). It allows to reload all environment variables. To check that our new variable is reachable in system, we can use following command:

In config/database.yml we can now use our environment variable:

 

 3. Bootstrap

You can download bootstrap here: http://getbootstrap.com/getting-started/

Extract downloaded pack and copy:

– bootstrap.css and bootstrap.min.css to vendor/assets/stylesheets

– bootstrap.js and bootstrap.min.js to vendor/assets/javascripts

– font folder (folder and files inside) to vendor/assets (it should give you: vendor/assets/fonts/glyphicons-halflings-regular.eot)

Update following files by adding:

– app/assets/stylesheets/application.css :

– app/assets/javascripts/application.js :

Place it in right place, after jquery:

Add bootstrap.min after jquery and jquery_ujs. If you would add it before, you get some problem with using bootstrap compoments.

– app/assets/stylesheets/application.css:

At the end of file, add following section. It allows to use imported fonts:

 

 

4. Partials in view – bootstrap navigation bar

In this section, we show how to add a bootstrap navigation bar as view partial.

Create file: app/views/layouts/_navbar.html.erb. Paste to this file some code. We use a responsive navigation bar from bootstrap. Here you can find source code: http://getbootstrap.com/components/#navbar-default, which can be copied-pasted to _navbar.html.erb.

Add to this partial to app/views/layouts/application.html.erb:

 

5. Models relations

HAS_MANY and BELONGS_TO

Task belongs to topic and topic has many  tasks.

 

 

And it’s necessary to add an attribute of parent to child model:

 

 

Tomasz AntasRails briefly