0
\$\begingroup\$

Rails 7.1.2

I have a form to create a new Employer that exists on the Employers#index view

# app/views/employers/index.html.erb

<div class="p-3">
  <h1 class="text-2xl">Add an Employer</h1>
  <%= form_with scope: @employer, url: employers_path, method: :post, data: { controller: "reset-form", action: "turbo:submit-end->reset-form#reset" },class: "flex justify-center" do |form| %>
    <table class="table" id="add_employer">
      <tr>
        <td><%= form.label :name %></td>
        <td><%= form.text_field :name %></td>
      </tr>
      <tr>
        <td><%= form.label :city %></td>
        <td><%= form.text_field :city %></td>
      </tr>
      <tr>
        <td><%= form.label :state %></td>
        <td><%= form.text_field :state %></td>
      </tr>
      <tr>
        <td><%= form.label :website %></td>
        <td><%= form.text_field :website %></td>
      </tr>
      <tr>
        <td><%= form.label :industry %></td>
        <td><%= form.text_field :industry %></td>
      </tr>
      <tr>
        <td><%= form.label :employees %></td>
        <td><%= form.text_field :employees %></td>
      </tr>
      <tr class="text-center" id="submit-button-tr">
        <td></td>
        <td class="p-3"><%= form.submit "Add company", class: "btn btn-blue" %></td>
      </tr>
    </table>
  <% end %>
</div>

The form will send a POST request to Employers#create action in the Employers_controller:

# app/controllers/employers_controller.rb

  def create
    @employer = Employer.new(employer_params)

    @pagy, @employers = pagy(Employer.sort_by_name)

    respond_to do |format|
      if @employer.save
        format.turbo_stream { flash.now[:success] = "The employer #{@employer.name} was successfully added." }
      else
        format.turbo_stream { flash.now[:alert] = "Please try again" }
      end
    end
  end

The Employer validates for all fields before persisting to the database:

# app/models/employer.rb

class Employer < ApplicationRecord
  scope :sort_by_name, -> { distinct(:name).order(:name) }

  validates :name, :city, :state, :website, :industry, :employees, presence: true
end

The Employers#index view is being updated via Turbo Streams upon successful/unsuccessful creation:

# create.turbo_stream.erb

<%= turbo_stream.update "employers" do %>
  <table class="table-fixed">
    <thead>
      <tr>
        <th>Employer Name</th>
        <th>City</th>
        <th><%= link_to "State", sort: "state" %></th>
        <th>Industry</th>
        <th># of Employees</th>
      </tr>
    </thead>
    <tbody>
      <% @employers.each do |employer| %>
        <tr>
          <td><%= link_to "#{employer.name}", employer.website %></td>
          <td><%= employer.city %></td>
          <td><%= employer.state %></td>
          <td><%= employer.industry %></td>
          <td><%= employer.employees %></td>
        </tr>
      <% end %>
    </tbody>
  </table>
<% end %>

<%= turbo_stream.update "flash" do %>
  <%= render  partial: "layouts/flash" %>
<% end %>

I had to use turbo frames/turbo streams and a _flash partial to get the flash message to render, since I want Employers#index to update automatically upon successful/unsuccessful persistence to the database.

My primary question is: Is there a better way to render the flash messages that doesn't use turbo streams?

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.