Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
Last active March 9, 2021 13:36
<%= form_for(board, html: {class: "space-y-4"}) do |form| %>
<div>
<%= form.label :title, class: "block text-sm font-medium text-gray-700" %>
<%= form.text_field :title %>
</div>
<div>
<%= form.fields_for :embeds do |embed_form| %>
<%= embed_form.label :input, class: "sr-only" %>
<%= embed_form.text_field :input, placeholder: "Enter an embeddable link", data: {reflex_permanent: true} %>
<% end %>
<div class="relative">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<%= button_tag
data: {reflex: "click->NestedForm#add", resource: "Board", nested_resource: "Embed"} do %>
<svg class="-ml-1.5 mr-1 h-5 w-5 text-gray-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd" />
</svg>
<span>Add Embed</span>
<% end %>
</div>
</div>
</div>
<%= form.submit" %>
<% end %>
class BoardsController < ApplicationController
#...
def new
@board = session.fetch(:forms, {}).fetch("Board", Board.new)
end
def create
@board = Board.new(board_params)
respond_to do |format|
if @board.save
session[:forms].delete "Board"
format.html { redirect_to @board, notice: 'Board was successfully created.' }
else
format.html { render :new }
end
end
end
# ...
end
class NestedFormReflex < ApplicationReflex
def add
session[:forms] ||= {}
session[:forms]["Board"] ||= Board.new
session[:forms]["Board"].embeds.build
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment