Check rails version
Create a rails project
Start the server for Rails
Start rails Access "http: // localhost: 3000 or localhost: 3000".
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
 Ruby on Rails Tutorial: Learn Rails with Examples https://railstutorial.jp/
Recommended Posts