Rails learning day 1 part 3

Ruby on Rails Quick Learning Practice Guide chapter3

3-1-4 Use properly for each database environment

There are three types of environments in the database. A database is also allocated according to each task.

Environment type Environmental system name Use
Development environment development Check the operation during development
testing environment test Perform automated testing
Production environment production Make it available to users

At the time of development, we will use the development environment and the test environment. These two files are automatically created when you execute db: create.

3-1-5 Make it possible to use slim to write the view layer efficiently (how to use slim)

By default, if you change the template engine used in erb to a template engine called slim, you can easily write code.

How to write slim

How to write erb How to write slim
<% name %> - name
<%= name %> = name
#comment /comment

Hello </p>

p Hello
<a href='//example.com">image</a> a href="//example.com"image
<div class="profile name"> .profile.name
<div id="pam"> #pam

Slim is convenient when you get used to it, so it will be difficult until you get used to it.

3-2-2 Create a template for the task model

・ Create a model To create a model, enter what you want to create after rails g (this time model)

$ rails g model Task name:string description:text

By doing this ・ Task model can be created -Create a blueprint migration file that can create a table with name: string description: text -Create a file for automatic model testing

To create a table, db: migrate the migration file to create the table.

3-3 Controller and view

·routing Routing looks at the URL and HTTP method and assigns it to each action of the controller.

URL example HTTP method Action name Function name role
/tasks GET index List display Show all tasks
/tasks/17 GET show Detail View Show tasks with a specific id
/tasks/new GET new New registration screen New registration screenを表示する
/tasks POST create Registration Registration処理を行う
/tasks/17/edit GET edit Editing screen Editing screenを表示する
/tasks/17 PATCH,PUT update update update処理を行う
/tasks/17 DELETE destory Delete Delete処理を行う

HTTP methods are important! !!

・ How to combine routing into one

get 'tasks/index'
get 'tasks/show'
get 'tasks/new'
get 'tasks/edit'

Summarize the above routing using resources

resource :tasks

The resources method can combine the seven actions of index, show, new, create, edit, update and destroy into one.

get "/" => "tasks#index"

root to: 'tasks#index'

Also, if you specify the initial screen of rails as root to, the view linked to that action will be the initial screen.

・ URL helper method You can also replace / tasks / new, / tasks / edit, etc. with helper methods

URL example HTTP method URL pattern name URL helper method
/tasks GET tasks tasks_path
/tasks/17 GET task task_path
/tasks/new GET new_task new_task_path
/tasks POST tasks tasks_path
/tasks/17/edit GET edit_task edit_task_path
/tasks/17 PATCH、PUT task task_path
/tasks/17 DELETE task task_path

3-3-1-5 Implement the view of the new registration screen

New registration screen

ruby:app/views/tasks/new.html.slim


New registration of h1 task

.nav.justify-content-end
  = link_to 'List', tasks_path, class: 'nav-link'

=form_with model:@task,local:true do |f|
  .form-group
    =f.label :name 
    =f.text_field :name, class: 'form-control', id: 'task_name'

  .form-group
    =f.label :name 
    =f.text_area :description, rows: 5, class: 'form-control', id: 'task_description'
  =f.submit nil, class: 'btn btn-primary'

form_with is for creating a form. Fill out the required form using f. ・ Explanation .nav.justify-content-end: Code provided by Bootstrap f.label: name: Display the label corresponding to the input field Originally name, but since it has been translated into Japanese, it will be named name = f.text_field: name: Where to put the text class:'form-control: The class originally attached to bootstrap

3-3-1-6 Implement the registration action

app/controllers/tasks_controller.rb


def create
   task = Task.new(task_params)
   task.save!
   redirect_to tasks_url, notice: "task"#{task.name}Was registered"
end



private

def task_params
   params.require(:task).permit(:name,:description)
end

require (: task) .permit (: name,: description) means "create a new task by allowing only the name and description information". Get the created parameter with params.

3-3-3 Implement the detailed display function

tasks / [task id] can be expressed as task_path (task) using the URL helper method. The id is automatically determined from the argument task in rails.

3-3-3-2 Display task attribute information on the detail screen

simple_format(h(@task.description),{},sanitize: false, wrapper_tag: "div")

Sentences that include explanations, such as description, become long and require line breaks. Line breaks are easily performed by using simple_format.

Recommended Posts

Rails learning day 1 part 3
Rails learning day 3 part 2
Rails learning Day 1 Part 2
Rails learning day 4
Rails learning day 2
rails learning day 1
Programming learning day 3
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
java learning day 2
java learning day 1
Rails Tutorial Chapter 3 Learning
[Rails] Learning with Rails tutorial
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
4th day of java learning
Ruby on Rails basic learning ①
Ruby on rails learning record-2020.10.07 ②
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Rails 5 Code Reading Part 2 ~ Action View ~
rails learning rake task, cron, whenever
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
part of the syntax of ruby ​​on rails
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
Rails Tutorial 6th Edition Learning Summary Chapter 3
Muscle Ruby on Rails Day 1 ~ Environment Construction ~
Rails Tutorial 6th Edition Learning Summary Chapter 8