SlideShare a Scribd company logo
Multiple radio buttons
Aka join table nightmares
Multiple radio buttons
● Scenario and schema
● Rails collection_radio_buttons
● Rails solution
● Rails + SCSS + JS solution
● Links
Scenario and schema
Scenario
A typical context that needs multiple radio button options on a single page
● Multiple users adding review feedback
● Multiple things to be reviewed
Example sites with these capabilities
● TripAdvisor
○ Hotels
○ Restaurants
● GoodReads
○ Books
Book Review Schema
Note to self: review join table architecture
Rails collection_radio_buttons
<input type="radio" value="2" name="user[books_user][score]" id="user_books_user_score_2">
Rails collection_radio_buttons
Rails provides a helper, collection_radio_buttons
<%= @books.each do |book| %>
<%= f.fields_for book.books_users.create do |ff| %>
<%= f.collection_radio_buttons( :score, BooksUser.scores, :id) %>
<% end %>
<% end %>
https://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_radio_buttons
Rails solution
View
<%= form_for(@user, html: { method: :put }) do |f| %>
...
<% @books.each do |book| %>
<%= f.fields_for "books_user][#{book.id}" do |ff| %>
<%= ff.collection_radio_buttons :score, BooksUser.scores, :last, :last do |rb| %>
<span title=<%= rb.object[0].humanize %>><%= rb.radio_button %></span>
<% end %>
...
Put > update
Build hidden field
Use span for inline
Chrome debugger > elements tab
Chrome debugger > network tab > form data
Form param syntax - best practice?
Is the param syntax correct / best practice?
● I have no idea
User[books_user][4][score] or User[books_user][user_id][4][score] or … ?
● Possible to build any syntax you want
● As built:
○ Succinct
○ Straightforward to parse in the controller
Controller
def update
book_params[:books_user].each do |param|
next unless param.last[:score]&.present?
BooksUser.create(user_id: @user.id, score: param.last[:score].to_i,
book_id: param.first.to_i)
end
redirect_to user_path(@user)
end
def book_params
params.require(:user).permit(books_user: :score)
end
View / controller - best practice?
Is it correct / best practice to use edit / put / update?
● I have no idea
The controller code creates join entries
● Natural fit would be new / post / create, BUT
● Create optimised for single create, not multiple
● Both the books and the user already exist
○ We’re only gluing them together with a join table entry
Rails + SCSS + JS solution
(without collection_radio_buttons)
View
<% @books.each do |book| %>
<%= f.fields_for "books_user][#{book.id}" do |ff| %>
<td class=”book-score” book=<%= book.id %>>
<!-- CSS uses right-to-left, so star index is reversed, ie from 4 to 0 -->
<% star_index = 4 %>
<% 5.times do %>
<span class=”icon” onClick=”clickStar()” score=<%= star_index
%>></span>
<% star_index -= 1 %>>
<% end %>
...
Stash the book id for JS to find
JS entry point
SCSS
.book-score {
unicode-bidi: bidi-override;
direction: rtl;
}
.icon.scored:before, .icon.scored ~ span:before,
.icon.hover:before, .icon.hover ~ span:before {
content: “f005”
color: steelblue;
}
Apologies for missing credit
https://www.w3docs.com/snippets/css/what-does-the-css-tilde-selector-mean.html
JS
function clickStar() {
var name = “user[books_user][“ + bookId + “][score]”
var input = document.createElement(“input”)
input.setAttribute(“type, “hidden”, “name”, name, “value”, score)
oldScore.classList.remove(“scored”)
event.target.classList.toggle(“scored”)
}
Links
Links
Collection radio buttons
https://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_radio_buttons
Githup repo (public)
https://github.com/egglestn/multiple
Heroku live demo
https://multiple-radio-buttons.herokuapp.com/

More Related Content

What's hot

日本語の語彙平易化システムおよび評価セットの構築
日本語の語彙平易化システムおよび評価セットの構築日本語の語彙平易化システムおよび評価セットの構築
日本語の語彙平易化システムおよび評価セットの構築
Tomoyuki Kajiwara
 
GEE(一般化推定方程式)の理論
GEE(一般化推定方程式)の理論GEE(一般化推定方程式)の理論
GEE(一般化推定方程式)の理論
Koichiro Gibo
 
連続時間フラクショナル・トピックモデル(NLP2023 金融・経済ドメインのための言語処理)
連続時間フラクショナル・トピックモデル(NLP2023 金融・経済ドメインのための言語処理)連続時間フラクショナル・トピックモデル(NLP2023 金融・経済ドメインのための言語処理)
連続時間フラクショナル・トピックモデル(NLP2023 金融・経済ドメインのための言語処理)
Kei Nakagawa
 
StanとRで折れ線回帰──空間的視点取得課題の反応時間データを説明する階層ベイズモデルを例に──【※Docswellにも同じものを上げています】
StanとRで折れ線回帰──空間的視点取得課題の反応時間データを説明する階層ベイズモデルを例に──【※Docswellにも同じものを上げています】StanとRで折れ線回帰──空間的視点取得課題の反応時間データを説明する階層ベイズモデルを例に──【※Docswellにも同じものを上げています】
StanとRで折れ線回帰──空間的視点取得課題の反応時間データを説明する階層ベイズモデルを例に──【※Docswellにも同じものを上げています】
Hiroyuki Muto
 
(2021.3) 不均一系触媒研究のための機械学習と最適実験計画
(2021.3) 不均一系触媒研究のための機械学習と最適実験計画(2021.3) 不均一系触媒研究のための機械学習と最適実験計画
(2021.3) 不均一系触媒研究のための機械学習と最適実験計画
Ichigaku Takigawa
 
[DL輪読会] Hybrid computing using a neural network with dynamic external memory
[DL輪読会] Hybrid computing using a neural network with dynamic external memory[DL輪読会] Hybrid computing using a neural network with dynamic external memory
[DL輪読会] Hybrid computing using a neural network with dynamic external memory
Yusuke Iwasawa
 
SPARQLでオープンデータ活用!
SPARQLでオープンデータ活用!SPARQLでオープンデータ活用!
SPARQLでオープンデータ活用!
uedayou
 
機械学習~データを予測に変える技術~で化学に挑む! (サイエンスアゴラ2021)
機械学習~データを予測に変える技術~で化学に挑む! (サイエンスアゴラ2021)機械学習~データを予測に変える技術~で化学に挑む! (サイエンスアゴラ2021)
機械学習~データを予測に変える技術~で化学に挑む! (サイエンスアゴラ2021)
Ichigaku Takigawa
 

What's hot (8)

日本語の語彙平易化システムおよび評価セットの構築
日本語の語彙平易化システムおよび評価セットの構築日本語の語彙平易化システムおよび評価セットの構築
日本語の語彙平易化システムおよび評価セットの構築
 
GEE(一般化推定方程式)の理論
GEE(一般化推定方程式)の理論GEE(一般化推定方程式)の理論
GEE(一般化推定方程式)の理論
 
連続時間フラクショナル・トピックモデル(NLP2023 金融・経済ドメインのための言語処理)
連続時間フラクショナル・トピックモデル(NLP2023 金融・経済ドメインのための言語処理)連続時間フラクショナル・トピックモデル(NLP2023 金融・経済ドメインのための言語処理)
連続時間フラクショナル・トピックモデル(NLP2023 金融・経済ドメインのための言語処理)
 
StanとRで折れ線回帰──空間的視点取得課題の反応時間データを説明する階層ベイズモデルを例に──【※Docswellにも同じものを上げています】
StanとRで折れ線回帰──空間的視点取得課題の反応時間データを説明する階層ベイズモデルを例に──【※Docswellにも同じものを上げています】StanとRで折れ線回帰──空間的視点取得課題の反応時間データを説明する階層ベイズモデルを例に──【※Docswellにも同じものを上げています】
StanとRで折れ線回帰──空間的視点取得課題の反応時間データを説明する階層ベイズモデルを例に──【※Docswellにも同じものを上げています】
 
(2021.3) 不均一系触媒研究のための機械学習と最適実験計画
(2021.3) 不均一系触媒研究のための機械学習と最適実験計画(2021.3) 不均一系触媒研究のための機械学習と最適実験計画
(2021.3) 不均一系触媒研究のための機械学習と最適実験計画
 
[DL輪読会] Hybrid computing using a neural network with dynamic external memory
[DL輪読会] Hybrid computing using a neural network with dynamic external memory[DL輪読会] Hybrid computing using a neural network with dynamic external memory
[DL輪読会] Hybrid computing using a neural network with dynamic external memory
 
SPARQLでオープンデータ活用!
SPARQLでオープンデータ活用!SPARQLでオープンデータ活用!
SPARQLでオープンデータ活用!
 
機械学習~データを予測に変える技術~で化学に挑む! (サイエンスアゴラ2021)
機械学習~データを予測に変える技術~で化学に挑む! (サイエンスアゴラ2021)機械学習~データを予測に変える技術~で化学に挑む! (サイエンスアゴラ2021)
機械学習~データを予測に変える技術~で化学に挑む! (サイエンスアゴラ2021)
 

Similar to Multiple radio buttons

Where's My SQL? Designing Databases with ActiveRecord Migrations
Where's My SQL? Designing Databases with ActiveRecord MigrationsWhere's My SQL? Designing Databases with ActiveRecord Migrations
Where's My SQL? Designing Databases with ActiveRecord Migrations
Eleanor McHugh
 
Schema plus
Schema plusSchema plus
Schema plus
Michał Łomnicki
 
Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)
lazyatom
 
Rails vu d'un Javaiste
Rails vu d'un JavaisteRails vu d'un Javaiste
Rails vu d'un Javaiste
Christian Blavier
 
Boston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on RailsBoston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on Rails
John Brunswick
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
fiyuer
 
Creating effective ruby gems
Creating effective ruby gemsCreating effective ruby gems
Creating effective ruby gems
Ben Zhang
 
Tableau course curriculum
Tableau course curriculumTableau course curriculum
Tableau course curriculum
Madhukar Reddy
 
Vancouver League of Drupallers - Remembering the User (August 2008)
Vancouver League of Drupallers - Remembering the User (August 2008)Vancouver League of Drupallers - Remembering the User (August 2008)
Vancouver League of Drupallers - Remembering the User (August 2008)
baronmunchowsen
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
James Casey
 
SF Elixir Meetup - RethinkDB
SF Elixir Meetup - RethinkDBSF Elixir Meetup - RethinkDB
SF Elixir Meetup - RethinkDB
Peter Hamilton
 
REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101
Samantha Geitz
 
Elastic tire demo
Elastic tire demoElastic tire demo
Elastic tire demo
Scott Hamilton
 
Create an application with ember
Create an application with ember Create an application with ember
Create an application with ember
Chandra Sekar
 
PostgreSQL Materialized Views with Active Record
PostgreSQL Materialized Views with Active RecordPostgreSQL Materialized Views with Active Record
PostgreSQL Materialized Views with Active Record
David Roberts
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
apostlion
 
Introduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in RIntroduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in R
Paul Richards
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon Rails
Paul Pajo
 
GHC
GHCGHC
GHC
AidIQ
 
MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)
Mike Dirolf
 

Similar to Multiple radio buttons (20)

Where's My SQL? Designing Databases with ActiveRecord Migrations
Where's My SQL? Designing Databases with ActiveRecord MigrationsWhere's My SQL? Designing Databases with ActiveRecord Migrations
Where's My SQL? Designing Databases with ActiveRecord Migrations
 
Schema plus
Schema plusSchema plus
Schema plus
 
Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)
 
Rails vu d'un Javaiste
Rails vu d'un JavaisteRails vu d'un Javaiste
Rails vu d'un Javaiste
 
Boston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on RailsBoston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on Rails
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
Creating effective ruby gems
Creating effective ruby gemsCreating effective ruby gems
Creating effective ruby gems
 
Tableau course curriculum
Tableau course curriculumTableau course curriculum
Tableau course curriculum
 
Vancouver League of Drupallers - Remembering the User (August 2008)
Vancouver League of Drupallers - Remembering the User (August 2008)Vancouver League of Drupallers - Remembering the User (August 2008)
Vancouver League of Drupallers - Remembering the User (August 2008)
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
SF Elixir Meetup - RethinkDB
SF Elixir Meetup - RethinkDBSF Elixir Meetup - RethinkDB
SF Elixir Meetup - RethinkDB
 
REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101
 
Elastic tire demo
Elastic tire demoElastic tire demo
Elastic tire demo
 
Create an application with ember
Create an application with ember Create an application with ember
Create an application with ember
 
PostgreSQL Materialized Views with Active Record
PostgreSQL Materialized Views with Active RecordPostgreSQL Materialized Views with Active Record
PostgreSQL Materialized Views with Active Record
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
 
Introduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in RIntroduction to Shiny for building web apps in R
Introduction to Shiny for building web apps in R
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon Rails
 
GHC
GHCGHC
GHC
 
MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)
 

Recently uploaded

Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 

Recently uploaded (20)

Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 

Multiple radio buttons

  • 1. Multiple radio buttons Aka join table nightmares
  • 2. Multiple radio buttons ● Scenario and schema ● Rails collection_radio_buttons ● Rails solution ● Rails + SCSS + JS solution ● Links
  • 4. Scenario A typical context that needs multiple radio button options on a single page ● Multiple users adding review feedback ● Multiple things to be reviewed Example sites with these capabilities ● TripAdvisor ○ Hotels ○ Restaurants ● GoodReads ○ Books
  • 5. Book Review Schema Note to self: review join table architecture
  • 7. <input type="radio" value="2" name="user[books_user][score]" id="user_books_user_score_2">
  • 8. Rails collection_radio_buttons Rails provides a helper, collection_radio_buttons <%= @books.each do |book| %> <%= f.fields_for book.books_users.create do |ff| %> <%= f.collection_radio_buttons( :score, BooksUser.scores, :id) %> <% end %> <% end %> https://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_radio_buttons
  • 10.
  • 11. View <%= form_for(@user, html: { method: :put }) do |f| %> ... <% @books.each do |book| %> <%= f.fields_for "books_user][#{book.id}" do |ff| %> <%= ff.collection_radio_buttons :score, BooksUser.scores, :last, :last do |rb| %> <span title=<%= rb.object[0].humanize %>><%= rb.radio_button %></span> <% end %> ... Put > update Build hidden field Use span for inline
  • 12. Chrome debugger > elements tab
  • 13. Chrome debugger > network tab > form data
  • 14. Form param syntax - best practice? Is the param syntax correct / best practice? ● I have no idea User[books_user][4][score] or User[books_user][user_id][4][score] or … ? ● Possible to build any syntax you want ● As built: ○ Succinct ○ Straightforward to parse in the controller
  • 15. Controller def update book_params[:books_user].each do |param| next unless param.last[:score]&.present? BooksUser.create(user_id: @user.id, score: param.last[:score].to_i, book_id: param.first.to_i) end redirect_to user_path(@user) end def book_params params.require(:user).permit(books_user: :score) end
  • 16. View / controller - best practice? Is it correct / best practice to use edit / put / update? ● I have no idea The controller code creates join entries ● Natural fit would be new / post / create, BUT ● Create optimised for single create, not multiple ● Both the books and the user already exist ○ We’re only gluing them together with a join table entry
  • 17. Rails + SCSS + JS solution (without collection_radio_buttons)
  • 18.
  • 19. View <% @books.each do |book| %> <%= f.fields_for "books_user][#{book.id}" do |ff| %> <td class=”book-score” book=<%= book.id %>> <!-- CSS uses right-to-left, so star index is reversed, ie from 4 to 0 --> <% star_index = 4 %> <% 5.times do %> <span class=”icon” onClick=”clickStar()” score=<%= star_index %>></span> <% star_index -= 1 %>> <% end %> ... Stash the book id for JS to find JS entry point
  • 20. SCSS .book-score { unicode-bidi: bidi-override; direction: rtl; } .icon.scored:before, .icon.scored ~ span:before, .icon.hover:before, .icon.hover ~ span:before { content: “f005” color: steelblue; } Apologies for missing credit https://www.w3docs.com/snippets/css/what-does-the-css-tilde-selector-mean.html
  • 21. JS function clickStar() { var name = “user[books_user][“ + bookId + “][score]” var input = document.createElement(“input”) input.setAttribute(“type, “hidden”, “name”, name, “value”, score) oldScore.classList.remove(“scored”) event.target.classList.toggle(“scored”) }
  • 22. Links
  • 23. Links Collection radio buttons https://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_radio_buttons Githup repo (public) https://github.com/egglestn/multiple Heroku live demo https://multiple-radio-buttons.herokuapp.com/