Skip to content

Commit

Permalink
make it work without resetting form
Browse files Browse the repository at this point in the history
  • Loading branch information
gobijan committed Dec 15, 2023
1 parent aea365e commit 818a721
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
/.tags
/.tags_sorted_by_file
/vendor/bundle/
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ gem "puma", ">= 5.0"
gem "importmap-rails"

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
# gem "turbo-rails", "~> 2.0.0.pre.beta"
gem "turbo-rails", github: "hotwired/turbo-rails"

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
Expand Down
17 changes: 11 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
GIT
remote: https://github.com/hotwired/turbo-rails.git
revision: 75b25b5e25d0cf22b7eaf82fd8c5fbad3e256057
specs:
turbo-rails (2.0.0.pre.beta.1)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -222,10 +231,6 @@ GEM
stringio (3.1.0)
thor (1.3.0)
timeout (0.4.1)
turbo-rails (1.5.0)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
web-console (4.2.1)
Expand Down Expand Up @@ -260,12 +265,12 @@ DEPENDENCIES
sprockets-rails
sqlite3 (~> 1.4)
stimulus-rails
turbo-rails
turbo-rails!
tzinfo-data
web-console

RUBY VERSION
ruby 3.2.2p53

BUNDLED WITH
2.4.22
2.5.0
29 changes: 29 additions & 0 deletions app/controllers/todos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class TodosController < ApplicationController
def index
@todos = Todo.all
end

def create
@todo = Todo.new(todo_params)
@todo.save
redirect_to todos_path
end

def update
@todo = Todo.find(params[:id])
@todo.update(completed: !@todo.completed)
redirect_to todos_path
end

def destroy
@todo = Todo.find(params[:id])
@todo.destroy
redirect_to todos_path
end

private

def todo_params
params.require(:todo).permit(:title)
end
end
2 changes: 2 additions & 0 deletions app/helpers/todos_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TodosHelper
end
5 changes: 5 additions & 0 deletions app/models/todo.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class Todo < ApplicationRecord
validates :title, presence: true
validates :completed, inclusion: {in: [true, false]}

broadcasts_refreshes
end

1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<%= yield :head %>
</head>

<body>
Expand Down
17 changes: 17 additions & 0 deletions app/views/todos/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>Todos</h1>

<%= turbo_refreshes_with method: :morph, scroll: :preserve %>

<div data-turbo-permanent>
<%= form_with model: Todo.new do |f| %>
<%= f.text_field :title %>
<%= submit_tag "Create Todo" %>
<% end %>
</div>

<%= turbo_stream_from "todos" %>
<ul>
<% @todos.each do |todo| %>
<li><%= todo.title %></li>
<% end %>
</ul>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
get "up" => "rails/health#show", as: :rails_health_check

# Defines the root path route ("/")
# root "posts#index"
root "todos#index"
resources :todos
end
21 changes: 21 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/controllers/todos_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class TodosControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit 818a721

Please sign in to comment.