CSC/ECE 517 Fall 2013/ch1 1w12 vn: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 26: Line 26:
|- style="vertical-align:top;"
|- style="vertical-align:top;"
| <%= debug @users %>
| <%= debug @users %>
| debug method gives a well-formatted output as opposed to methods above.
| !ruby/object:User
---
- !ruby/object:User
   attributes:
   attributes:
     id: 2
     id: 2

Revision as of 20:55, 16 September 2013

Introduction

Rails is an open-source web application framework which makes use of Ruby programming language. With its huge library of gems and support for MVC architecture, it provides an easy and clean approach for creating pages, talking to web server and dealing with databases. Debugging is always an important part of any application development for which Rails provides a tremendously good support despite being an interpreted language as opposed to C or Java or any other compiled language.

Debugging Options

Rails provides a range of options to make debugging easier. Some of these options have been discussed in the sections below.

Debug Helpers

One of the easiest ways to debug is to simply output the value of different variables which provides us the first look into what could be going wrong. This could be done in all of models, views and controllers. Rails provides three methods-debug, to_yaml and inspect to achieve this task. These methods create human-readable data from any object [1]. The examples below show the output when these methods are used in a view.

Code in index.html.erb

Code Output
<%= @users.to_yaml %> --- - !ruby/object:User attributes: id: 2 name: Jimmy Page email: jimmy.page@gmail.com created_at: 2013-09-14 02:37:16.322638000 Z updated_at: 2013-09-16 20:38:04.250678000 Z
<%= @users.inspect %> #<ActiveRecord::Relation [#<User id: 2, name: "Jimmy Page", email: "jimmy.page@gmail.com", created_at: "2013-09-14 02:37:16", updated_at: "2013-09-16 20:38:04">]>
<%= debug @users %> !ruby/object:User
 attributes:
   id: 2
   name: Jimmy Page
   email: jimmy.page@gmail.com
   created_at: 2013-09-14 02:37:16.322638000 Z
   updated_at: 2013-09-16 20:38:04.250678000 Z




Output


--- - !ruby/object:User

 attributes:
   id: 2
   name: Jimmy Page
   email: jimmy.page@gmail.com
   created_at: 2013-09-14 02:37:16.322638000 Z
   updated_at: 2013-09-16 20:38:04.250678000 Z