Model
The Model layer carries the business logic of the application and the rules to manipulate the data. In Ruby on Rails, the models are used to manage the interaction with their corresponding elements in the database. The Models represent the information in the database and do the appropriate validations.
View
The view is the front-end of the application, representing the user interface. In Ruby on Rails, views are HTML files with embedded Ruby code. The embedded Ruby code in the HTMLs is fairly simple (loops and conditionals). It is only used to display data to the user in the form of views. Views are used to provide the data to the browsers that requested the web pages. Views can server content in several formats, such as HTML, PDF, XML, RSS and more.
Controller
Controllers interact with models and views. The incoming requests from the browsers are processed by the controllers, which process the data from the models and pass it to the views for presentation.
# install rails
gem install rails
# check rails version
rails -v
# create new application "blog"
rails new blog
# run rails application
cd blog
rails server
Contains the controllers, models, views, helpers, mailers and assets for your application. You'll focus on this folder for the remainder of this guide.
Contains the rails script that starts your app and can contain other scripts you use to setup, update, deploy or run your application.
Configure your application's routes, database, and more.
Rails.application.routes.draw do
get 'posts/index'
get "posts_list", to: "posts#list", as: :post_list # <%= link_to "List", post_list_path %>
root "posts#index"
# Example of regular route:
get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
resources :blogs
# Example resource route with concerns:
concern :toggleable do
post 'toggle'
end
resources :posts, concerns: :toggleable
resources :photos, concerns: :toggleable
# Example resource route within a namespace:
namespace :admin do
# Directs /admin/products/* to Admin::ProductsController
# (app/controllers/admin/products_controller.rb)
resources :products
end
end
# my_app/config/routes.rb
Rails.application.routes.draw do
get 'post/index'
root "post#index"
resources :blogs
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
post_index GET /post/index(.:format) post#index
root GET / post#index
blogs GET /blogs(.:format) blogs#index
POST /blogs(.:format) blogs#create
new_blog GET /blogs/new(.:format) blogs#new
edit_blog GET /blogs/:id/edit(.:format) blogs#edit
blog GET /blogs/:id(.:format) blogs#show
PATCH /blogs/:id(.:format) blogs#update
PUT /blogs/:id(.:format) blogs#update
DELETE /blogs/:id(.:format) blogs#destroyExtended modules for your application.
Application log files.
Contains your current database schema, as well as the database migrations.
The only folder seen by the world as-is. Contains static files and compiled assets.
A place for all third-party code. In a typical Rails application this includes vendored gems.
Temporary files (like cache and pid files).
config.ru - Rack configuration for Rack based servers used to start the application.
$ rails generate scaffold Post title:string content:textWith the scaffold action, Rails generates all the code it needs dynamically. By running scaffold as a script, we can get all the code written to disk, where we can investigate it and then start tailoring it to our requirements.
invoke active_record
create db/migrate/20161026073039_create_posts.rb
create app/models/post.rb
invoke test_unit
create test/models/post_test.rb
create test/fixtures/posts.yml
invoke resource_route
route resources :posts
invoke scaffold_controller
create app/controllers/posts_controller.rb
invoke erb
create app/views/posts
create app/views/posts/index.html.erb
create app/views/posts/edit.html.erb
create app/views/posts/show.html.erb
create app/views/posts/new.html.erb
create app/views/posts/_form.html.erb
invoke test_unit
create test/controllers/posts_controller_test.rb
invoke helper
create app/helpers/posts_helper.rb
invoke test_unit
invoke jbuilder
create app/views/posts/index.json.jbuilder
create app/views/posts/show.json.jbuilder
create app/views/posts/_post.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/posts.coffee
invoke scss
create app/assets/stylesheets/posts.scss
invoke scss
identical app/assets/stylesheets/scaffolds.scss create app/controllers/posts_controller.rb
route get "posts/hello"
invoke erb
create app/views/posts
create app/views/posts/hello.html.erb
invoke test_unit
create test/controllers/posts_controller_test.rb
invoke helper
create app/helpers/posts_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/posts.coffee
invoke scss
create app/assets/stylesheets/posts.scssGenerate controller
$ rails generate controller Posts helloinvoke active_record
create db/migrate/20161026072633_create_posts.rb
create app/models/post.rb
invoke test_unit
create test/models/post_test.rb
create test/fixtures/posts.ymlGenerate model
$ rails g model post title:string content:textGit is a version control system that is used for software development and other version control tasks. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows.
GitHub is a web-based Git repository hosting service. It offers all of the distributed version control and source code management (SCM) functionality of Git as well as adding its own features.
$ sudo yum install git-allor
$ sudo apt-get install git-allAn OSX Git installer is maintained and available for download at the Git website, at http://git-scm.com/download/mac.
# checking git version
$ git version
# set user name
$ git config --global user.name "John Doe"
# set user email
$ git config --global user.email johndoe@example.com
# get config info
$ git config --list
user.name=John Doe
user.email=johndoe@example.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...$ cd your app
# Initializing a Repository in an Existing Directory
$ git init
# Check status
$ git status
# Addition of files
$ git add .
# Addition your commit
$ git commit -m "Name Of Your Commit"
# Set a new remote
$ git remote add origin https://github.com/user/repo.git
# Verify new remote
$ git remote -v
# Cloning from repository
$ git clone https://github.com/user/your_app.git
# Get list of local branches
$ git branch
# Add new branch
$ git checkout -b origin your_new_branch
# Checkout on other branch
$ git checkout my_branch
# Pull origin from repository
$ git pull origin other_branch
# Push your branch
$ git push origin my_brach
# Push with force
$ git push -f origin my_branch
# Get list of commits on branch
$ git log
# Rebase branches
$ git rebase origin other_branch
# Merge branches
$ git merge other_origin
# Reset your changes
$ git reset commit_id
# Force reset your changes
$ git reset -f commit_id
# Revert your commit by id
$ git revert commit_idLinks:
Books: