I learned the rudimentary things along with MVC.
Reference: Progate Ruby on Rails
#Project creation
$rails new application name
#Start the server localhost:????Access by
$rails sever
#Create a controller
#rails generate controller controller name action name
$rails generate controller home top
#localhost:????/home/top
#The page is automatically generated by the command
Controller
Application name /app/controller/controller.rb
Homecontrollers.rb
class HomeController < ApplicationController
def top
end
end
Application name /config/routes.rb
routes.rb
Rails.application.routes.draw do
get"home/top" => "home#top"
#URL controller name # action name
end
Allow access to localhost: ???? / ranking
Try to create ranking at the transition destination In other words Try to describe the process to make the URL transition so that it can be accessed with localhost: ???? / ranking
Homecontrollers.rb
class HomeController < ApplicationController
def top
end
#Added corresponding routing and action ranking here
def ranking
end
end
URL | controller | action |
---|---|---|
home/top | home | top |
ranking | home | ranking |
routes.rb
Rails.application.routes.draw do
get"home/top" => "home#top"
#URL controller name # action name
get"ranking" => "home#about"
end
View Write in html.erb How to write Ruby in html.erb Impressions: Should I not use the principle? Code visibility may be poor
Example: Loop the list
html.erb
<%
lists = [
"good",
"bad"
]
%>
<% lists.each do|list|% >
<div class="list">
<% list %>
</div>
<% end %>
Homecontrollers.rb
class HomeController < ApplicationController
#@To put on
def top
@lists = [
"good",
"bad"
]
end
end
html.erb
<% @lists.each do|list|% >
<div class="list">
<% list %>
</div>
<% end %>
Conversion to a code that understands the migration DB SQL statement if possible [SELECT, INSERT, UPDATE, DELETE ] It may be quick to understand if you can write.
#rails generate This is long so g is OK
$rails g model Post contents:text
#Post ・ ・ ・ ・ ・ ・ ・ ・ When creating plural forms such as posts users, use the singular form.
#content:・ ・ ・ Column name
#text ・ ・ ・ ・ ・ ・ ・ ・ Data type
Created at the time when the above is executed Application name /db/migrate/yyyymmddhhmmss_create_posts.rb
contents
posts.rb
class CreatePosts < ActiveRecord::Migration[5.0]
def change
create_table :posts do |t|
t.text :content
t.timestamps
end
end
end
ActiveRecord::PendingMigrationError I think that the error can be solved by executing $ rails db: migrate.
If you access the page with the migration file present A migration error occurs.
You can write processing interactively
$rails console
>post = Post.new(content:"test")
#Post instance generation
#test the content of the posts table
>post.save
#Save Post instance to table
#Saved in DB Content column
>quit end
$rails console
>post = Post.first
>post.content
>posts = Post.all
# SELECT "posts".* FROM "posts"
>posts[0]
#id: 1,
#content: "Contents",
#created_at: Thu, 29 Oct 2020 16:14:30 JST +09:00,
#updated_at: Thu, 29 Oct 2020 16:14:30 JST +09:00>
> posts[0].content
=># "Contents"
Homecontrollers.rb
class HomeController < ApplicationController
#@Get information from DB
def top
@posts = Post.all
end
Recommended Posts