I implemented an authenticating system for my sample rails project. Learnt more about controllers . Will update more info in future !
Search This Blog
Thursday, August 30, 2012
Wednesday, August 29, 2012
ROR - things i think u must know!
database.yml
Although rails follows 'convention over configuration' you need to configure things like database before working on a project. database.yml is the database configuration file for any Ruby on Rails project. It is present under the config directory of your project root folder.
So what is yml ? its a file extension of YAML files.
YAML --> YAML ain't markup language. Yes , it is a recursive acronym :)
However when it was being developed YAML meant yet another markup language. It was then changed to current name to stress the importance of data rather than markup.
In nutshell its just a human readable data serialization format. NOTE: it uses strict spacing. I missed a space and managed to fix the error only after an hour !
RESTful way!
In rails we do applications that are RESTful.
A resource is generally associated with entities which can be created,read,updated,deleted ( CRUD operations ).
I will explain what is RESTful with an example,
suppose you have a website (www.vehicles.com) that deals about different vehicles.
Now,
www.vehicles.com/lorry/wheels/2
This is RESTful. It clearly means you are accessing the 2nd wheel of a lorry.
the same can be implemented as,
www.vehicles.com/getvehicleaslorry/parts/getpart?id=2
It looks horrible and it is certainly not RESTful.
In Rails RESTful way comes naturally. You tend to have a controller for a resource and actions like create,destroy follow.
Models and ActiveRecords.
Every model inherits from ActiveRecord::Base.
Understand that ActiveRecord is a module. Its like a namespace. Base is a class inside it.
The ActiveRecord::Base class gives methods to access the records/fields of the table corresponding to that model using objects of that model itself.
For example, we have a users table which has the field name.
Now in the model User.rb,
user=User.new(:name=>"raj")
will create an object user.
user.save
will save the object as a record in the users table.
Instead of two separate statements you can do
User.create(:name => "raj")
It will save create a record in user table having name "raj"
Although rails follows 'convention over configuration' you need to configure things like database before working on a project. database.yml is the database configuration file for any Ruby on Rails project. It is present under the config directory of your project root folder.
So what is yml ? its a file extension of YAML files.
YAML --> YAML ain't markup language. Yes , it is a recursive acronym :)
However when it was being developed YAML meant yet another markup language. It was then changed to current name to stress the importance of data rather than markup.
In nutshell its just a human readable data serialization format. NOTE: it uses strict spacing. I missed a space and managed to fix the error only after an hour !
RESTful way!
In rails we do applications that are RESTful.
A resource is generally associated with entities which can be created,read,updated,deleted ( CRUD operations ).
I will explain what is RESTful with an example,
suppose you have a website (www.vehicles.com) that deals about different vehicles.
Now,
www.vehicles.com/lorry/wheels/2
This is RESTful. It clearly means you are accessing the 2nd wheel of a lorry.
the same can be implemented as,
www.vehicles.com/getvehicleaslorry/parts/getpart?id=2
It looks horrible and it is certainly not RESTful.
In Rails RESTful way comes naturally. You tend to have a controller for a resource and actions like create,destroy follow.
Models and ActiveRecords.
Every model inherits from ActiveRecord::Base.
Understand that ActiveRecord is a module. Its like a namespace. Base is a class inside it.
The ActiveRecord::Base class gives methods to access the records/fields of the table corresponding to that model using objects of that model itself.
For example, we have a users table which has the field name.
Now in the model User.rb,
user=User.new(:name=>"raj")
will create an object user.
user.save
will save the object as a record in the users table.
Instead of two separate statements you can do
User.create(:name => "raj")
It will save create a record in user table having name "raj"
Tuesday, August 28, 2012
ROR falling deeper , hoping i could climb up
I feel am learning something about Rails continuously. But at the end of the day the clarity is missing. I would now tell one thing which i understood , which i feel everyone must know before learning rails.
The MVC architecture. Rails allows you to build applications that follow MVC architecture.
M--> Model --> contains business logic and database interactions
V--> View --> contains code for front end
C--> Controller --> contains the flow of the application. Links view and models.
So what happens when a HTTP request arrives??
- First it hits the routes.rb file where we have our routes specified
- After reaching routes it knows which action of which controller to hit. It hits that action.
- The controller then interacts with model if necessary and then view corresponding to that action is hit. View is rendered and displayed to the user !
More about rails in future posts !
Friday, August 24, 2012
ROR day 2
The amazing thing i was mentioning in last post is "Scaffolding" . Not just scaffolding, but how it adjusts so nicely .
I used,
rails generate scaffold stickynotes
what Rails did is create Stickynote.rb as model and stickynotes_controller.rb as controller. I was wondering how did it automatically made the model as singular. I decided to experiment and did this,
rails generate scaffold bus
and i half expected it to create a model called bu. But it made the model as bus.rb and the controller as buses_controller.rb. Great work pluralizing!!
It also underlines one important point. In rails its all about conventions.
Into my second day in learning rails i feel the learning curve is pretty steep. And the reason for that according to me is the stress on conventions. There are lots and lots of conventions adopted and to be followed.
Will post more about concepts in posts to come !
I used,
rails generate scaffold stickynotes
what Rails did is create Stickynote.rb as model and stickynotes_controller.rb as controller. I was wondering how did it automatically made the model as singular. I decided to experiment and did this,
rails generate scaffold bus
and i half expected it to create a model called bu. But it made the model as bus.rb and the controller as buses_controller.rb. Great work pluralizing!!
It also underlines one important point. In rails its all about conventions.
Into my second day in learning rails i feel the learning curve is pretty steep. And the reason for that according to me is the stress on conventions. There are lots and lots of conventions adopted and to be followed.
Will post more about concepts in posts to come !
Subscribe to:
Posts (Atom)