Skip to content

Commit

Permalink
implements turbo frames and turbo stream to quotes crud
Browse files Browse the repository at this point in the history
  • Loading branch information
dalima-dev committed Jul 21, 2024
1 parent 134f5b3 commit 869c342
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
11 changes: 9 additions & 2 deletions app/controllers/quotes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def create
@quote = Quote.new(quote_params)

if @quote.save
redirect_to quotes_path, notice: 'Quote was successfully created.'
respond_to do |format|
format.html { redirect_to quotes_path, notice: 'Quote was successfully created.' }
format.turbo_stream
end
else
render :new, status: :unprocessable_entity
end
Expand All @@ -35,7 +38,11 @@ def update

def destroy
@quote.destroy
redirect_to quotes_path, notice: 'Quote was successfully destroyed.'

respond_to do |format|
format.html { redirect_to quotes_path, notice: 'Quote was successfully destroyed.' }
format.turbo_stream
end
end

private
Expand Down
24 changes: 14 additions & 10 deletions app/views/quotes/_quote.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<div class="quote">
<%= link_to quote.name, quote_path(quote) %>
<div class="quote__actions">
<%= button_to "Delete",
quote_path(quote),
method: :delete,
<%= turbo_frame_tag quote do %>
<div class="quote">
<%= link_to quote.name,
quote_path(quote),
data: { turbo_frame: "_top" } %>
<div class="quote__actions">
<%= button_to "Delete",
quote_path(quote),
method: :delete,
class: "btn btn--light" %>
<%= link_to "Edit",
edit_quote_path(quote),
class: "btn btn--light" %>
<%= link_to "Edit",
edit_quote_path(quote),
class: "btn btn--light" %>
</div>
</div>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/quotes/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= turbo_stream.prepend "quotes", @quote %>
<%= turbo_stream.update Quote.new, "" %>
1 change: 1 addition & 0 deletions app/views/quotes/destroy.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= turbo_stream.remove @quote %>
4 changes: 3 additions & 1 deletion app/views/quotes/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
<div class="header">
<h1>Edit quote</h1>
</div>
<%= render "form", quote: @quote %>
<%= turbo_frame_tag @quote do %>
<%= render "form", quote: @quote %>
<% end %>
</main>
8 changes: 6 additions & 2 deletions app/views/quotes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<h1>Quotes</h1>
<%= link_to "New quote",
new_quote_path,
class: "btn btn--primary" %>
class: "btn btn--primary",
data: { turbo_frame: dom_id(Quote.new) } %>
</div>
<%= render @quotes %>
<%= turbo_frame_tag Quote.new %>
<%= turbo_frame_tag "quotes" do %>
<%= render @quotes %>
<% end %>
</main>
4 changes: 3 additions & 1 deletion app/views/quotes/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
<div class="header">
<h1>New quote</h1>
</div>
<%= render "form", quote: @quote %>
<%= turbo_frame_tag @quote do %>
<%= render "form", quote: @quote %>
<% end %>
</main>

0 comments on commit 869c342

Please sign in to comment.