$ 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>
Google Maps API Google Maps API | Google Developers https://developers.google.com/maps/
Google Developers Console https://console.developers.google.com/
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