$ rails new JMDMega
$ bin/rails server--> Check localhost:3000 -> Stop ctrl +C
$ bin/rails generate controller Welcome index
creates: welcome_controller, index function, index.html.erb,(welcome/index, default route)
<\app\controller\action_controller.rb>
class ApplicationController < ActionController::Base
end
<\app\controller\welcome_controller.rb>
class WelcomeController<ApplicationController
def index; end; end
<\view\welcome\index.html.erb>
<h1>Hello, Rails!</h1>
the first extension is the format of the template,
the second extension is the handler that will be used to render the template.
<\config\routes>
Rails.application.routes.draw do
get 'welcome/index'
root 'welcome#index'
<\config\routes.rb> - add resource: :articles --> this creates API CRUD routes
$bin/rails routes
| route Verb | URI | Action |
|---|---|---|
| root GET | / | welcome#index |
| welcome_index GET | /welcome/index(.:format) | welcome#index |
| . | articles GET | /articles(.:format) |
| . | POST | /articles(.:format) |
| . | new_article GET | /articles/new(.:format) |
| edit_article GET | /articles/:id/edit(.:format) | articles#edit |
article GET | /articles/:id(.:format) | articles#show|
.|PATCH | /articles/:id(.:format) | articles#update| .|PUT | /articles/:id(.:format) | articles#update .|DELETE | /articles/:id(.:format) | articles#destroy|
$ bin/rails generate controller Articles
create a new file at app/views/articles/new.html.erb
<h1>New Article<h1>
To create a form within this template, you will use a form builder. The primary form builder for Rails is provided by a helper method called form_with. The form needs to use a different URL in order to go somewhere else. This can be done quite simply with the :url option of form_with.
<%= form_with scope: :article, url: articles_path, local: true do |form| %>
def create ; render plain: params[:article].inspect ; end
Models in Rails use a singular name, and their corresponding database tables use a plural name.
$ bin/rails generate model Article title:string text:text
app/models/article.rb and db/migrate/20140120191729_create_articles.rb
class CreateArticles < ActiveRecord::Migration[5.0]
def change
create_table :articles do |t|
t.string :title
t.text :text
t.timestamps
Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it's possible to undo a migration after it's been applied to your database. Migration filenames include a timestamp to ensure that they are processed in the order that they were created.
$ bin/rails db:migrate
== CreateArticles: migrating ==================================================
-- create_table(:articles)
-> 0.0019s
== CreateArticles: migrated (0.0020s) =========================================
def create
@article = Article.new(params[:article])
#/ attritube mapped to respective db model
#/ class named Article that is defined in app/models/article.rb.
@article.save #/ returns boolean-the article was saved or not.
redirect_to @article #/ show action, which we'll define later
end
Rails exactly which parameters are allowed into our controller actions. #articles_controller.rb
@article = Article.new(article_params)
private
def article_params
params.require(:article).permit(:title, :text)
end
TODO
We've covered the "CR" part of CRUD. Now let's focus on the "U" part, updating articles. TODO
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MM- MVCR app
rails new project (creates defaults),
1. Welcome Page- CVR- no Model
`generate controller Welcome index`
add action index (), create <index.html.erb> View "<h1>Hello, Rails!</h1>",
add <config\routes.rb> `get 'welcome/index' `
resources :articles, root route)
2. route add `resource: :articles` (creates CRUD API routes)
`generate controller Articles `, <new.html.erb> View "New Art",
create form- view Articles using form_with
create action- #create
generate model Article title:string text:text
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
Output of $ bin/rails generate controller Welcome index
Running via Spring preloader in process 45551
create app/controllers/welcome_controller.rb
route get 'welcome/index'
invoke erb
create app/views/welcome
create app/views/welcome/index.html.erb
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/welcome.coffee
invoke scss
create app/assets/stylesheets/welcome.scss
$ bin/rails generate controller Articles
Running via Spring preloader in process 2146
create app/controllers/articles_controller.rb
invoke erb
create app/views/articles
invoke test_unit
create test/controllers/articles_controller_test.rb
invoke helper
create app/helpers/articles_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/articles.coffee
invoke scss
create app/assets/stylesheets/articles.scss
$ Generate scaffolding User name:string