Skip to content

Commit ea0a6ee

Browse files
committed
Quote scaffold with simple form
1 parent e5bca9f commit ea0a6ee

File tree

22 files changed

+648
-2
lines changed

22 files changed

+648
-2
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,5 @@ group :test do
6868
gem "capybara"
6969
gem "selenium-webdriver"
7070
end
71+
72+
gem "simple_form", "~> 5.3"

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ GEM
212212
rexml (~> 3.2, >= 3.2.5)
213213
rubyzip (>= 1.2.2, < 3.0)
214214
websocket (~> 1.0)
215+
simple_form (5.3.1)
216+
actionpack (>= 5.2)
217+
activemodel (>= 5.2)
215218
sprockets (4.2.1)
216219
concurrent-ruby (~> 1.0)
217220
rack (>= 2.2.4, < 4)
@@ -264,6 +267,7 @@ DEPENDENCIES
264267
puma (>= 5.0)
265268
rails (~> 7.1.3, >= 7.1.3.4)
266269
selenium-webdriver
270+
simple_form (~> 5.3)
267271
sprockets-rails
268272
stimulus-rails
269273
turbo-rails

app/controllers/quotes_controller.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class QuotesController < ApplicationController
2+
before_action :set_quote, only: [:show, :edit, :update, :destroy]
3+
4+
def index
5+
@quotes = Quote.all
6+
end
7+
8+
def show
9+
end
10+
11+
def new
12+
@quote = Quote.new
13+
end
14+
15+
def create
16+
@quote = Quote.new(quote_params)
17+
18+
if @quote.save
19+
redirect_to quotes_url, notice: "Quote was successfully created."
20+
else
21+
render :new, status: :unprocessable_entity
22+
end
23+
end
24+
25+
def edit
26+
end
27+
28+
def update
29+
if @quote.update(quote_params)
30+
redirect_to quotes_path, notice: "Quote was successfully updated."
31+
else
32+
render :edit, status: :unprocessable_entity
33+
end
34+
end
35+
36+
def destroy
37+
@quote.destroy
38+
redirect_to quotes_path, notice: "Quote was successfully destroyed."
39+
end
40+
41+
private
42+
43+
def set_quote
44+
@quote = Quote.find(params[:id])
45+
end
46+
47+
def quote_params
48+
params.require(:quote).permit(:name)
49+
end
50+
end

app/helpers/quotes_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module QuotesHelper
2+
end

app/models/quote.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Quote < ApplicationRecord
2+
validates :name, presence: true
3+
end

app/views/quotes/_form.html.erb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<%
2+
<<-COMMENT
3+
%>
4+
<%= form_with(model: quote) do |form| %>
5+
<% if quote.errors.any? %>
6+
<div style="color: red">
7+
<h2><%= pluralize(quote.errors.count, "error") %> prohibited this quote from being saved:</h2>
8+
9+
<ul>
10+
<% quote.errors.each do |error| %>
11+
<li><%= error.full_message %></li>
12+
<% end %>
13+
</ul>
14+
</div>
15+
<% end %>
16+
17+
<div>
18+
<%= form.submit %>
19+
</div>
20+
<% end %>
21+
<%
22+
COMMENT
23+
%>
24+
25+
<%= simple_form_for quote, html: { class: "quote form" } do |f| %>
26+
<% if quote.errors.any? %>
27+
<div class="error-message">
28+
<%= quote.errors.full_messages.to_sentence.capitalize %>
29+
</div>
30+
<% end %>
31+
32+
<%= f.input :name, input_html: { autofocus: true } %>
33+
<%= f.submit class: "btn btn--secondary" %>
34+
<% end %>

app/views/quotes/_quote.html.erb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<%
2+
<<-COMMENT
3+
%>
4+
<div id="<%= dom_id quote %>">
5+
</div>
6+
<%
7+
COMMENT
8+
%>
9+
10+
<div class="quote">
11+
<%= link_to quote.name, quote_path(quote) %>
12+
<div class="quote__actions">
13+
<%= button_to "Delete",
14+
quote_path(quote),
15+
method: :delete,
16+
class: "btn btn--light" %>
17+
<%= link_to "Edit",
18+
edit_quote_path(quote),
19+
class: "btn btn--light" %>
20+
</div>
21+
</div>

app/views/quotes/edit.html.erb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<%
2+
<<-COMMENT
3+
%>
4+
<h1>Editing quote</h1>
5+
6+
<%= render "form", quote: @quote %>
7+
8+
<br>
9+
10+
<div>
11+
<%= link_to "Show this quote", @quote %> |
12+
<%= link_to "Back to quotes", quotes_path %>
13+
</div>
14+
<%
15+
COMMENT
16+
%>
17+
18+
<main class="container">
19+
<%= link_to sanitize("&larr; Back to quote"), quote_path(@quote) %>
20+
21+
<div class="header">
22+
<h1>Edit quote</h1>
23+
</div>
24+
25+
<%= render "form", quote: @quote %>
26+
</main>

app/views/quotes/index.html.erb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<%
2+
<<-COMMENT
3+
%>
4+
<p style="color: green"><%= notice %></p>
5+
6+
<h1>Quotes</h1>
7+
8+
<div id="quotes">
9+
<% @quotes.each do |quote| %>
10+
<%= render quote %>
11+
<p>
12+
<%= link_to "Show this quote", quote %>
13+
</p>
14+
<% end %>
15+
</div>
16+
17+
<%= link_to "New quote", new_quote_path %>
18+
<%
19+
COMMENT
20+
%>
21+
22+
<main class="container">
23+
<div class="header">
24+
<h1>Quotes</h1>
25+
<%= link_to "New quote",
26+
new_quote_path,
27+
class: "btn btn--primary" %>
28+
</div>
29+
30+
<%= render @quotes %>
31+
</main>

app/views/quotes/new.html.erb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%
2+
<<-COMMENT
3+
%>
4+
<h1>New quote</h1>
5+
6+
<%= render "form", quote: @quote %>
7+
8+
<br>
9+
10+
<div>
11+
<%= link_to "Back to quotes", quotes_path %>
12+
</div>
13+
<%
14+
COMMENT
15+
%>
16+
17+
<main class="container">
18+
<%= link_to sanitize("&larr; Back to quotes"), quotes_path %>
19+
20+
<div class="header">
21+
<h1>New quote</h1>
22+
</div>
23+
24+
<%= render "form", quote: @quote %>
25+
</main>

0 commit comments

Comments
 (0)