SlideShare a Scribd company logo
1 of 23
Download to read offline
ancestry
アジェンダ
・ancestryとは
・ancestryの導入方法
・ancestryの実装方法 + 解説
・ancestryの簡略図
ancestryとは
ancestryとは、親子構造(ディレクトリ構造)を
簡単に作成することができるgemです。
今回では下の画像のように
カテゴリー検索をancestryで実装していきます。
→ →
→ →
親 子 孫
ancestryの簡略図
NULL 1 1/2
関係性
カラム
ancestryの導入方法
①Gemfileに「gem ancestry 」を記載し、「bundle install」
②データベースにancestryカラムを追加
③モデルに「has_ancestry」を追記
ancestryの導入方法は、大まかには以下のようになります。
ancestryの実装方法
ancestryを利用したプルダウンカテゴリーを実装していきます。
・「$ rails new ancestry -d mysql」
・「$ rails db:create」
・「$ rails db:migrate」
・Gemfileに以下を記載後、「bundle install」
・「$ rails haml:replace_erbs」でHamlに変換
・「$ rails g controller searches」でコントローラー作成
・ routes.rbに以下を追記
・「$ rails g model maincategory」でモデルを作成
・ application.jsを以下のように編集
・「$ rails g migration add_ancestry_to_maincategory
ancestry:string:index」でancestryカラムを追加
・「rails db:migrate」
・ 作成したマイグレーションファイルに「t.string :name」を追記
・ seeds.rbに以下のデータを貼り付け、「$ rails db:seed」を実行
https://techtechmedia.com/seeds-ancestry/
・ モデルに「has_ancestry」を追記
・「searches/new.html.haml」を以下のように編集
・「category.js」を作成し、以下のように編集
・「views/searches/new.json.jbuilder」を作成し、
以下のように編集
・「searches_controller.rb」を以下のように編集
解説 (カテゴリーが表示されるまでの流れ)
トップページを表示
① まずは「routes.rb」の「root searches#new 」が発火
②
↓ 検証ツールで確認すると、以下のようになっている
③ トップページ表示
④ カテゴリーを選択
paramsのキーを指定しているが、
送信ボタンがないためparamsは送られない
カテゴリー選択で発火する記述
value属性を取得
コントローラー(ルーティング経由で)
にキーを送る(例、 parent"=>"2")
⑤ カテゴリー選択を引き金にJSが発火
⑥ キーがコントローラーに送られ、JSON形式でレスポンスを返す
⑦ jbuilderでjson形式に変換される
⑧ doneで処理結果をビューに返す
下のプルダウンを表示する
↓ このように表示する
⑨さらにプルダウンが選択された時は、以下のJSが発火する
さらに下のプルダウンを表示する
↑ このように表示する
参考
f.collection_selectの書き方
Ancestry

More Related Content

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Ancestry