Monday, May 6, 2024
HomeRuby On RailsDiving into Hotwire | Drifting Ruby

Diving into Hotwire | Drifting Ruby


# Terminal
bundle add hotwire-rails
bin/rails webpacker:set up:stimulus
bin/rails turbo:set up
rails g scaffold merchandise title "worth:decimal{8,2}"
# fashions/product.rb
class Product < ApplicationRecord
  validates :title, presence: true, uniqueness: true
finish
# merchandise/edit.html.erb
<%= turbo_frame_tag 'product' do %>
  <h1>Modifying Product</h1>

  <%= render 'type', product: @product %>

  <%= link_to 'Present', @product %> |
  <%= link_to 'Again', products_path %>
<% finish %>
# merchandise/index.html.erb
<%= turbo_frame_tag 'product' do %>
  <h1>Merchandise</h1>
  <desk class="desk">
    <thead>
      <tr>
        <th>Title</th>
        <th>Value</th>
        <th colspan="3"></th>
      </tr>
    </thead>

    <tbody>
      <% @merchandise.every do |product| %>
        <tr>
          <td><%= product.title %></td>
          <td><%= product.worth %></td>
          <td><%= link_to 'Present', product %></td>
          <td><%= link_to 'Edit', edit_product_path(product) %></td>
          <td><%= link_to 'Destroy', product, methodology: :delete, knowledge: { affirm: 'Are you certain?' } %></td>
        </tr>
      <% finish %>
    </tbody>
  </desk>

  <br>

  <%= link_to 'New Product', new_product_path %>
<% finish %>
# merchandise/new.html.erb
<%= turbo_frame_tag 'product' do %>
  <h1>New Product</h1>

  <%= render 'type', product: @product %>

  <%= link_to 'Again', products_path %>
<% finish %>
# merchandise/present.html.erb
<%= turbo_frame_tag 'product' do %>
  <p id="discover"><%= discover %></p>

  <p>
    <sturdy>Title:</sturdy>
    <%= @product.title %>
  </p>

  <p>
    <sturdy>Value:</sturdy>
    <%= @product.worth %>
  </p>
  <%= link_to 'Edit', edit_product_path(@product) %> |
  <%= link_to 'Again', products_path %>
<% finish %>
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments