Ruby on rails learning record-2020.10.07 ①

Introducing Bootstrap to Rails Fixed Gemfile

gem 'bootstrap-sass', '~> 3.3.6'


gem 'sass-rails', '~> 5.0

Execute command $ bundle install

scss fix

Changed "app / assets / stylesheets / application.css" to "app / assets / stylesheets / application.css.scss"

app/assets/stylesheets/application.css.scss


@import 'bootstrap-sprockets';
@import 'bootstrap';

/* universal */

body {
  padding: 60px 15px 0;
}

Modify js file

app/assets/javascript/application.js


//= require bootstrap-sprockets

Allocate container

app/views/layouts/application.html.erb


<body>
    <div class='container'>
        <%= yield %>
    </div>
</body>

Responsive design in Bootstrap List table modification

app/views/shops/index.html.erb


<table class='table table-striped table-hover'>
・ ・ ・
</table>

Add a button to the link

app/views/shops/index.html.erb


<%= link_to 'New Shop', new_shop_path, class: 'btn btn-primary' %> | <%= link_to 'Show category list', categories_path %>

Form modification

app/views/shops/_form.html.erb


<%= form_for(shop, html: {class: 'form-horizontal', role: 'form'}) do |f| %>

app/views/shops/_form.html.erb


<div class='form-group'>
  <%= f.label :category_id, class: 'col-sm-2 control-label' %>
  <div class='col-sm-10'>
    <%= f.select :category_id, Category.all.map{|o| [o.name, o.id]} %>
  </div>
</div>

<div class='form-group'>
  <%= f.label :name, class: 'col-sm-2 control-label' %>
  <div class='col-sm-10'>
    <%= f.text_field :name %>
  </div>
</div>

<div class='form-group'>
  <%= f.label :address, class: 'col-sm-2 control-label' %>
  <div class='col-sm-10'>
    <%= f.text_field :address %>
  </div>
</div>

app/views/shops/_form.html.erb


<div class='actions'>
  <div class='form-group'>
    <div class='col-sm-offset-2 col-sm-10'>
      <%= f.submit 'Submit', class: 'btn btn-success' %>
    </div>
  </div>
</div>

Grid width ratio </ b> Arrange the elements to which "col-sm-3" is applied and the elements to which "col-sm-9" is applied. → The width ratio is 3: 9.

Make the screen for smartphones Add a navigation bar

app/views/layouts/application.html.erb


<!-- Fixed navbar -->
<nav class='navbar navbar-inverse navbar-fixed-top'>
  <div class='container'>
    <div class='navbar-header'>
      <button type='button' class='navbar-toggle collapsed' data-toggle='collapse' data-target='#navbar' aria-expanded='false' aria-controls='navbar'>
        <span class='sr-only'>Toggle navigation</span>
        <span class='icon-bar'></span>
        <span class='icon-bar'></span>
        <span class='icon-bar'></span>
      </button>
      <%= link_to 'Lunch Map', root_path, class: 'navbar-brand' %>
    </div>
    <div id='navbar' class='collapse navbar-collapse'>
      <ul class='nav navbar-nav'>
       <li><%= link_to 'Shop', shops_path %></li>
       <li><%= link_to 'Category', categories_path %></li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>

Click a row in the list to display the details page

app/views/shops/index.html.erb


<td><%= link_to shop.category.name, shop, class: 'widelink' %></td>
<td><%= link_to shop.name, shop, class: 'widelink' %></td>
<td><%= link_to shop.address, shop, class: 'widelink' %></td>

app/assets/stylesheets/application.css.scss


/* table row for link */
a.widelink {
   display: block;
   width: 100%;
   height: 100%;
   text-decoration: none;
}

Remove each list button from the list

app/views/shops/index.html.erb


<table class='table table-striped table-hover'>
  <thead>
    <tr>
      <th>Category</th>
      <th>Name</th>
      <th>Address</th>
    </tr>
  </thead>

Fit the map on the screen

app/views/shops/show.html.erb


<%= content_tag(:iframe,
    'map',
    src:'https://www.google.com/maps/embed/v1/place?key=AIzaSyCJBgcuCowQa5-V8owXaUCHhUNBN8bfMfU&amp;q=' + @shop.address,
    width: '100%',
    height: 320,
    frameborder: 0) %>

Added button to detail page

app/views/shops/show.html.erb


<%= link_to 'Delete', @shop,
                    method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' %>
<%= link_to 'Edit', edit_shop_path(@shop), class: 'btn btn-primary' %>
<%= link_to 'Back', shops_path, class: 'btn btn-default' %>

Added a button to return to the registration page

app/views/shops/_form.html.erb


<div class='actions'>
  <div class='form-group'>
    <div class='col-sm-offset-2 col-sm-10'>
      <%= f.submit 'Submit', class: 'btn btn-success' %>
      <%= link_to 'Back', :back, class: 'btn btn-default' %>
    </div>
  </div>
</div>

Added new and refresh page buttons

app/views/shops/edit.html.erb


<h1>Editing Shop</h1>

<%= render 'form', shop: @shop %>

<!-- <%= link_to 'Show', @shop %> | -->
<!-- <%= link_to 'Back', shops_path %> -->

app/views/shops/new.html.erb


<h1>New Shop</h1>

<%= render 'form', shop: @shop %>

<!-- <%= link_to 'Back', shops_path %> -->

Japanese localization of Rails Fixed controller to switch to match URL options

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  before_action :set_locale
  protect_from_forgery with: :exception

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  def default_url_options(options = {})
    { locale: I18n.locale }.merge options
  end
end

Copy ja.yml and fix $ cp -a config/locales/en.yml config/locales/ja.yml

/config/locale/ja.yml


ja:
  hello: "World of Hello, everyone"

Embed translation keywords on the welcome page

app/views/welcome/index.html.erb


<h1>Lunch Map</h1>
<p>Tasty meal on your place!!</p>
<p><%= t('hello') %></p>
<p><%= link_to 'Show shop list', shops_path %></p>

Embed translation keywords in the store list

app/views/shops/index.html.erb


<h1><%= t('shops') %></h1>

<th><%= t('category') %></th>
<th><%= t('name') %></th>
<th><%= t('address') %></th>

Register English words in en.yml

/config/locale/en.yml


en:
  hello: 'Hello world'
  shops: 'Shops'
  category: 'Category'
  name: 'Name'
  address: 'Address'

Register Japanese in ja.yml

/config/locale/ja.yaml


ja:
  hello: 'World of Hello, everyone'
  shops: 'Shop list'
  category: 'category'
  name: 'Store name'
  address: 'Street address'

Add data to ja.yml for form

/config/locale/ja.yaml


ja:
  hello: 'World of Hello, everyone'
  shops: 'Shop list'
  category: 'category'
  name: 'Store name'
  address: 'Street address'

  activerecord:
    models:
      shop:shop

    attributes:
      shop:
        category_id:category
        name:Store name
        address:Street address

Recommended Posts

Ruby on rails learning record -2020.10.03
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 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 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] 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 Learning # 25 Comments
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)
Ruby Learning # 34 Modules
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails