Ruby on Rails Elementary

Ruby on Rails Elementary

I learned the rudimentary things along with MVC.

Reference: Progate Ruby on Rails

Project creation

#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

Actually use

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

If you tabulate the routing, it looks like this.

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 %>

When storing variables in Controller and executing

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 %>

DB preparation Model

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

error

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.

About rails console

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

The process of retrieving data from a table

$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

Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
[Ruby on Rails] Introduced paging function
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
Tailwind on Rails
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command
[Ruby on Rails] About bundler (for beginners)
part of the syntax of ruby ​​on rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
[Ruby on Rails] Follow function implementation: Bidirectional
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Image slideshow using Skippr
Ruby on Rails controller create / delete command
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] About Active Record callbacks
Ruby on Rails application new creation command
[Ruby on Rails] Japanese notation of errors
Ruby on Rails6 Practical Guide cp4 ~ cp6 [Memo]