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.
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.
・ 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.
·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 |
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
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.
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.
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