Revising old stuff and few basics
- Rails is a web application framework which follows MVC pattern and based on Ruby scripting language.
- When we execute command rails project_name it creates a directory structure and set of classes and ruby files. Its basically a fully functional web application and web server.
- We can run ruby web server (webrick) by going to project_name folder and running ruby script/server command. It will start ruby webrick HTTP server on port 3000. If we want to use any other port then we need to add -p argument followed by port number.
- Rails apps follows Convention over Configurations. That means, All rails application follow the basic file structure and consistent names for things. This enable Rails to understand how application works and that way framework can automate certain tasks.
- To edit default page navigate, change HTML file public/index.html, public folder contains all the static content for the application. Like JS, CSS, HTML etc.
- Most of the time, when dealing with database in a web application, we need CRUD operations, which is Create, Read, Update and Delete.
- ruby script/generate scaffold book name:string isbn:string author:string price:decimal summary:text This will scaffold book entity and write basic code for manipulating books data. It creates controller, model and view for CRUD operations for books.
- Rails follows DRY (Don’t Repeat Yourself) principal. That means if we tell something to rails once, we shouldn’t tell it again.
- When rail generated scaffold code, it also created a script called a migration to create a table. In db/migrate folder, it created a file _create_books.rb
- Using rake db:migrate we can ask rails to migrate database changes. Being smart, it will check and execute changes which are pending. In other words, if it execute a db migration script earlier, it won’t execute same.
- rake = Ruby’s make
- To add new fields in table, we need to generate a migration. For eg. if we want to add a new field publisher:string in our books table, then command will be: ruby script/generate migration AddPublisherToBooks publisher:string this will generate a new migration which will have alteration for table. Then we need to call rake db:migrate and it will make changes in database. However, in view we need to add new field manually.