Ruby on rails learning record -2020.10.06

Create a store introduction site Automatic generation of shop list

$ rails generate scaffold shop category_id:integer name:string address:string


$ rails db:migrate'```

 Association of category strike and store list model


#### **`app/model/category.rb`**
```rb

class Category < ApplicationRecord
  has_many :shops
end

app/models/shop.rb


class Shop < ApplicationRecord
  belongs_to :category
end

Allows you to select a category on the registration / modification form

app/views/shops/_form.html.erb


<div class="field">
  <%= f.label :category_id %>
  <%= f.select :category_id, Category.all.map{|o| [o.name, o.id]} %>
</div>

Display the category name in the list display and detailed display of the store list

app/view/shops/index.html.erb


<td><%= shop.category.name %></td>

app/view/shops/show.html.erb


app/view/shops/show.html.erb

Add search form to view

/app/views/shops/index.html.erb


<%= form_tag('/shops', method: 'get') do %>
<%= label_tag(:name_key, 'Search name:') %>
<%= text_field_tag(:name_key) %>
<%= submit_tag('Search') %> <%= link_to 'Clear', shops_path %>
<% end %>

<br>

Modify index method in controller

/app/controllers/shops_controller.rb


def index
  if params[:name_key]
    @shops = Shop.where('name LIKE ?', "%#{params[:name_key]}%")
  else
    @shops = Shop.all
  end
end

Link from the welcome page

app/views/welcome/index.html.erb


<h1>Lunch Map</h1>
<p>Tasty meal on your place!!</p>
<p><%= link_to 'Show shops', shops_path %></p>

Incorporate Google Maps API Abbreviation for Application Programming Interface An instruction or function provided to call another function from a program.

Google Maps API Google Maps API | Google Developers https://developers.google.com/maps/

API key acquisition procedure
  1. Access the Google Developers Console

Google Developers Console https://console.developers.google.com/

  1. Select Create Project
  2. When the Google API is displayed, select "Google Maps Embed API" from the Google Maps API.
  3. Click "Enable"
  4. Click "Create Credentials" and then click the "Required Credentials" button
  5. Record the displayed API key
  • It is recommended to set "API usage restrictions" so that it can be used only from specific Web services.
  • This procedure and scope of use are subject to change on the Google side.

Add map area to view

app/views/shops/show.html.erb


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

<br>

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.06
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 Rails6) "Erase" posted content
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] 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] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
Ruby Learning # 25 Comments
[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 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
Ruby on Rails model creation / deletion command
Tailwind on Rails