Specify version 6.0.0 of rails. Specify the DB (mysql in this case) to be used after -d.
rails _6.0.0_ new birdtweet -d mysql
... # encoding: utf8mb4 encoding: utf8 ...
cd birdtweet rails db:create
Editing Gemfile Open the Gemfile directly under the project
# gem 'mysql2', '>= 0.4.4' gem 'mysql2', '>= 0.5.3' ........ (Bottom line of file) gem 'pry-rails'
Update settings in the terminal
rails g model bird
... create_table :birds do |t| t.string :name t.text :pic t.timestamps end ...
Update your changes with commands!
Put some data in the DB at the console.
rails c Bird.create(name: "Puffin", pic: "https://cdn.pixabay.com/photo/2020/05/26/13/22/puffins-5223057_1280.jpg ") Bird.create(name: "Kingfisher", pic: "https://cdn.pixabay.com/photo/2017/02/07/16/47/kingfisher-2046453_1280.jpg ") exit
rails g controller birds
Pluralized or not, confusing, Only the model is singular!
Added to routes.rb ↓ Added links to each function in index.html.erb (unexpected index) ↓ Added to birds_controller.rb ↓ Function name.html.erb creation
If you do not want to use all the functions, specify only the ones to be used with the only option.
#Behind resources is the plural of the model name resources :birds, only: :index
... def index #Store the Bird model data in an instance variable. @birds = Bird.all end ...
index.html.erb file creation Display all instance variables in the view using the each method.
<% @birds.each do |bird| %> <%= bird.name %> <div style= "background-image: url(<%= bird.pic %>); background-position: center center; background-size: cover; width: 300px; height: 300px; margin-bottom: 10px; "> </div> <%end%>
It was displayed as below! !! !!
Rails.application.routes.draw do #Behind resources is the plural of the model name #If there are multiple actions, I will make it in the form of an array resources :birds, only: [:index, :new] end
<%=link_to 'New post', new_bird_path, method: :get%> ...
... def new #Store the instantiated version of the Bird model in an instance variable. @bird = Bird.new end ...
<%#Even if you do not describe the transition destination url, it will transition based on the model put in the instance variable.%> <%=form_with(model: @bird, local: true) do |form| %> <%= form.text_field :name, placeholder: "Bird name" %> <%= form.text_field :pic, placeholder: "Bird photo URL" %> <%= form.submit "Post" %> <%end%>
http://localhost:3000/birds/new You can see the posting screen when you access!
Next is the function to register the input information.
Rails.application.routes.draw do #Behind resources is the plural of the model name #If there are multiple actions, I will make it in the form of an array resources :birds, only: [:index, :new, :create] end
The data will be sent in the form of a hash from the form.
It is dangerous to receive all the data! </ b>
Even though only the name and URL of the photo are required, the login information key can be maliciously added, and the login information of another person can be changed and hijacked without permission. So we use strong parameters.
Also, below the line that describes private, it is a method that cannot be called from other files. The advantage is that you can see fewer files when you have more methods.
... def create #bird defined under private_Receives the parameter specified by param and saves it. Bird.create(bird_param) end private def bird_param # params.require(:Model name).permit(:Column name,:Column name,......) params.require(:bird).permit(:name, :pic) end ...
<h3>Posting completed!</h3> <a href="/birds">To List</a>
If you leave it as it is, you will be able to register without entering anything. Therefore, I will write a description for validation check.
class Bird < ApplicationRecord #Write the column name you want to be required to enter. validates :name, presence: true validates :pic, presence: true end
You can no longer register the data from now on! !!
... resources :birds, only: [:index, :new, :create, :destroy] ...
To find the link to the delete function, first use the command.
will do. Then, the following output will be output. If you add "_path" to the Prefix value, the URL described in the URI will be output.
This time it's deleted, so pay attention to the line that says DELETE in the verb. Prefix is "bird", so you can set it to "bird_path". The method is "DELETE" in "Verb", so you can just say "delete". After that, pass the id as well!
... <%= bird.name %><%=link_to "Delete", bird_path(bird.id), method: :delete%> ...
The link is displayed.
... def destroy #This time I will not send the extracted data anywhere, [email protected] not attach. bird = Bird.find(params[:id]) bird.destroy end ...
<a href="/birds">List screen</a>
Puffin has been removed properly!