Run #create action

This commit is contained in:
cryptogopher 2024-01-06 03:59:09 +01:00
parent ba3c2a99b9
commit ce4770a25c
6 changed files with 35 additions and 26 deletions

View File

@ -20,7 +20,7 @@ class UnitsController < ApplicationController
@unit = current_user.units.new(unit_params)
if @unit.save
flash[:notice] = t(".success")
redirect_to units_url
@units = current_user.units
else
render :new
end

View File

@ -1,21 +1,22 @@
<%= fields_for unit do |form| %>
<%= fields_for @unit do |form| %>
<td>
<%= form.text_field :symbol, form: :unit_form, required: true, autofocus: true, size: 10,
maxlength: unit.class.columns_hash['symbol'].limit, autocomplete: "off" %>
maxlength: @unit.class.columns_hash['symbol'].limit, autocomplete: "off" %>
</td>
<td>
<%= form.text_field :name, form: :unit_form, size: 25,
maxlength: unit.class.columns_hash['name'].limit, autocomplete: "off" %>
maxlength: @unit.class.columns_hash['name'].limit, autocomplete: "off" %>
</td>
<td>
<% unless unit.base.nil? %>
<% unless @unit.base.nil? %>
<%= form.number_field :multiplier, form: :unit_form, step: "any", size: 10,
autocomplete: "off" %>
<% end %>
</td>
<td class="actions">
<%= form.submit unit.persisted? ? t(:update) : t(:add), form: :unit_form %>
<%= form.submit @unit.persisted? ? t(:update) : t(:add), form: :unit_form,
onclick: 'focusAddLink(event);' %>
<%= image_link_to t(:cancel), "close-circle-outline", units_path, class: 'dangerous',
onclick: 'closeForm(event); return false;' %>
</td>
@ -25,6 +26,10 @@
<%= javascript_tag do %>
function closeForm(event) {
event.target.closest("tr").replaceChildren();
focusAddLink(event);
}
function focusAddLink(event) {
var add_unit_link = document.querySelector("a#add_unit");
add_unit_link.style.visibility = "visible";
add_unit_link.focus({ focusVisible: true });

View File

@ -0,0 +1,17 @@
<tr id="<%= dom_id(Unit.new) %>" is="turbo-frame"></tr>
<% Unit.each_with_level(@units) do |unit, level| %>
<tr>
<td class="link">
<%= link_to unit.symbol, edit_unit_path(unit), class: level > 0 ? 'subunit' : '' %>
</td>
<td><%= unit.name %></td>
<td class="number"><%= scientifize(unit.multiplier) unless unit.multiplier == 1 %></td>
<% if current_user.at_least(:active) %>
<td class="actions">
<%= image_button_to t(".delete_unit"), "delete-outline", unit_path(unit),
method: :delete %>
</td>
<% end %>
</tr>
<% end %>

View File

@ -0,0 +1,3 @@
<%= turbo_stream.update :units do %>
<%= render partial: 'index' %>
<% end %>

View File

@ -9,7 +9,7 @@
<%= turbo_frame_tag 'unit_form_frame' %>
<table class="main items" id="units">
<table class="main items">
<thead>
<tr>
<th><%= User.human_attribute_name(:symbol).capitalize %></th>
@ -20,23 +20,7 @@
<% end %>
</tr>
</thead>
<tbody>
<tr id="<%= dom_id(Unit.new) %>" is="turbo-frame"></tr>
<% Unit.each_with_level(@units) do |unit, level| %>
<tr>
<td class="link">
<%= link_to unit.symbol, edit_unit_path(unit), class: level > 0 ? 'subunit' : '' %>
</td>
<td><%= unit.name %></td>
<td class="number"><%= scientifize(unit.multiplier) unless unit.multiplier == 1 %></td>
<% if current_user.at_least(:active) %>
<td class="actions">
<%= image_button_to t(".delete_unit"), "delete-outline", unit_path(unit),
method: :delete %>
</td>
<% end %>
</tr>
<% end %>
<tbody id="units" is="turbo-frame">
<%= render partial: 'index' %>
</tbody>
</table>

View File

@ -1,5 +1,5 @@
<%= turbo_stream.update @unit do %>
<%= render partial: 'form', locals: {unit: @unit} %>
<%= render partial: 'form' %>
<% end %>
<%= turbo_stream.update :unit_form_frame do %>