Ruby on rails learning record -2020.10.03

Ruby on rails basic commands

Check rails version

Create a rails project

Start the server for Rails

Start rails Access "http: // localhost: 3000 or localhost: 3000".

Creating a bulletin board Automatically generate a bulletin board ``` $ rails generate scaffold article content:string $ rails db:migrate ```

Access the bulletin board to be created "Articles" describes the app name specified by the rails scaffold command in plural form. Go to "localhost: 3000 / articles". To do.

When representing multiple collections (collections of DB rows, etc.), they are plural, and when there is only one (class (model controller), etc.), they are singular.

Creating a Welcome page $ rails generate controller welcome [page name] index

Visit the Welcome page. 「localhost:3000/welcome/index」

Set on the top page.

config/routes.rb


Rails.application.routes.draw do
  get 'welcome/index'

  resources :articles
  root 'welcome#index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

How to modify a web page

app/views/welcome/index.html.erb


<h1>Hello BBS</h1>
<p><%= Date.today %></p>
<%= link_to 'Show list', articles_path %>

-MVC architecture -Model: Holds and operates the data handled by the application. -View: Display the received data. -Controller: Handles requests from users, calls model views and returns results.

How to add columns Add name column to database articles table

string


$ rails db:migtare

Modify the view file

index.html.erb


<table>
  <thead>
    <tr>
      <th>Content</th>
      <th>Name</th>
      <th colspan="3"></th>
    </tr>
  </thead>
  <tbody>
    <% @articles.each do |article| %>
      <tr>
        <td><%= article.content %></td>
        <td><%= article.name %></td>
        <td><%= link_to 'Show', article %></td>
        <td><%= link_to 'Edit', edit_article_path(article) %></td>
        <td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

show.html.erb


<p>
  <strong>Name:</strong>
  <%= @article.name %>
</p>

_from.html.erb


<div class="field">
  <%= f.label :name %>
  <%= f.text_field :name %>
</div>

Fix controller

article_controller.rb


def article_params
    params.require(:article).permit(:content, :name)
end

Reference Get started with Rails | Rails Guide https://railsguides.jp/getting_started.html

Ruby on Rails Tutorial: Learn Rails with Examples https://railstutorial.jp/

Recommended Posts

Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on rails learning record-2020.10.07 ②
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Ruby on Rails basic learning ①
Ruby on Rails Elementary
Ruby on Rails basics
[Ruby on Rails] About Active Record callbacks
Ruby On Rails Association
Portfolio creation Ruby on Rails
[Ruby on Rails] Debug (binding.pry)
Ruby on Rails config configuration
[Ruby on Rails] about has_secure_password
Commentary on partial! --Ruby on Rails
Cancel Ruby on Rails migration
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Ruby learning 4
Ruby learning 5
Ruby on Rails5 Quick Learning Practice Guide 5.2 Compatible Chapter2
Ruby learning 3
Ruby learning 2
Ruby on Rails5 Quick Learning Practice Guide 5.2 Compatible Chapter3
Ruby learning 6
Ruby learning 1
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
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
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] What is Bcrypt?
[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 ..."
Ruby on Rails record search, create if not find_or_create_by method
[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 Rails6 Practical Guide cp13 ~ cp15 [Memo]
Ruby Learning # 13 Arrays
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] Code check using Rubocop-airbnb
Ruby Learning # 1 Introduction
[Ruby on Rails] 1 model CRUD (Routing Main)