Skip to content

Commit 4097a44

Browse files
authored
Merge pull request #70 from capotej/error_handling
crush: fix error handling
2 parents 1d4b2b8 + 3b00d36 commit 4097a44

File tree

9 files changed

+54
-28
lines changed

9 files changed

+54
-28
lines changed

app/controllers/blog_controller.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,21 @@ def feed_by_tag
5353
end
5454

5555
def create
56-
post = Post.create! post_params
57-
redirect_to dated_post_path(year: post.year, month: post.month, day: post.day, id: post.slug)
56+
@post = Post.new(post_params)
57+
if @post.save
58+
redirect_to dated_post_path(year: @post.year, month: @post.month, day: @post.day, id: @post.slug)
59+
else
60+
render :new, status: :unprocessable_content
61+
end
5862
end
5963

6064
def update
61-
post = Post.find_by_slug(params[:id])
62-
post.markdown_body_attachments.purge
63-
if post.update(post_params)
64-
redirect_to dated_post_path(year: post.year, month: post.month, day: post.day, id: post.slug)
65+
@post = Post.find_by_slug(params[:id])
66+
@post.markdown_body_attachments.purge
67+
if @post.update(post_params)
68+
redirect_to dated_post_path(year: @post.year, month: @post.month, day: @post.day, id: @post.slug)
6569
else
66-
render :edit, status: :unprocessable_entity
70+
render :edit, status: :unprocessable_content
6771
end
6872
end
6973

app/controllers/feeds_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def create
1414
if @feed.save
1515
redirect_to feeds_path, notice: "Feed was successfully created."
1616
else
17-
render :new
17+
render :new, status: :unprocessable_content
1818
end
1919
end
2020

@@ -25,7 +25,7 @@ def update
2525
if @feed.update(feed_params)
2626
redirect_to feeds_path, notice: "Feed was successfully updated."
2727
else
28-
render :edit
28+
render :edit, status: :unprocessable_content
2929
end
3030
end
3131

app/controllers/links_controller.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ def feed
2222
end
2323

2424
def create
25-
link = Link.create! link_params
26-
redirect_to links_path
25+
@link = Link.new(link_params)
26+
if @link.save
27+
redirect_to links_path
28+
else
29+
render :new, status: :unprocessable_content
30+
end
2731
end
2832

2933
def update
3034
@link = Link.find(params[:id])
3135
if @link.update(link_params)
3236
redirect_to links_path
3337
else
34-
render :edit, status: :unprocessable_entity
38+
render :edit, status: :unprocessable_content
3539
end
3640
end
3741

app/controllers/pages_controller.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ def index
1919
end
2020

2121
def create
22-
post = Page.create! page_params
23-
redirect_to post
22+
@page = Page.new(page_params)
23+
if @page.save
24+
redirect_to @page
25+
else
26+
render :new, status: :unprocessable_content
27+
end
2428
end
2529

2630
def update
@@ -29,7 +33,7 @@ def update
2933
if @page.update(page_params)
3034
redirect_to @page
3135
else
32-
render :edit, status: :unprocessable_entity
36+
render :edit, status: :unprocessable_content
3337
end
3438
end
3539

app/views/blog/_form.html.erb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<%= form_with model: post, class: "space-y-6" do |form| %>
2-
3-
<% post.errors.each do |error| %>
4-
<div><%= error.full_message %></div>
5-
<% end %>
2+
<%= render "shared/form_errors", object: post %>
63

74
<div class="space-y-2">
85
<%= form.label :title, class: "block text-sm font-medium text-gray-700 dark:text-gray-200" %>

app/views/feeds/_form.html.erb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<%= form_with model: feed, class: "space-y-6" do |form| %>
2-
<% feed.errors.each do |error| %>
3-
<div><%= error.full_message %></div>
4-
<% end %>
2+
<%= render "shared/form_errors", object: feed %>
53

64
<div class="space-y-2">
75
<%= form.label :name, class: "block text-sm font-medium text-gray-700 dark:text-gray-200" %>

app/views/links/_form.html.erb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<%= form_with model: link, class: "space-y-6" do |form| %>
2-
<% link.errors.each do |error| %>
3-
<div><%= error.full_message %></div>
4-
<% end %>
2+
<%= render "shared/form_errors", object: link %>
53

64
<div class="space-y-2">
75
<%= form.label :url, class: "block text-sm font-medium text-gray-700 dark:text-gray-200" %>

app/views/pages/_form.html.erb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<%= form_with model: page, class: "space-y-6" do |form| %>
2-
<% page.errors.each do |error| %>
3-
<div><%= error.full_message %></div>
4-
<% end %>
2+
<%= render "shared/form_errors", object: page %>
53

64
<div class="space-y-2">
75
<%= form.label :title, class: "block text-sm font-medium text-gray-700 dark:text-gray-200" %>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<% if object.errors.any? %>
2+
<div class="rounded-lg border border-red-200 bg-red-50 p-4 mb-6 dark:border-red-800 dark:bg-red-950/50 transition-colors">
3+
<div class="flex">
4+
<div class="flex-shrink-0">
5+
<svg class="h-5 w-5 text-red-500 dark:text-red-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
6+
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
7+
</svg>
8+
</div>
9+
<div class="ml-3 flex-1">
10+
<h3 class="text-sm font-semibold text-red-800 dark:text-red-200">
11+
<%= pluralize(object.errors.count, "error") %> prohibited this <%= object.class.model_name.human.downcase %> from being saved
12+
</h3>
13+
<div class="mt-3 text-sm text-red-700 dark:text-red-300">
14+
<ul role="list" class="list-disc space-y-1 pl-5 marker:text-red-400 dark:marker:text-red-500">
15+
<% object.errors.each do |error| %>
16+
<li class="pl-1"><%= error.full_message %></li>
17+
<% end %>
18+
</ul>
19+
</div>
20+
</div>
21+
</div>
22+
</div>
23+
<% end %>

0 commit comments

Comments
 (0)