Skip to content

Commit

Permalink
Use 4XX instead of 2XX for validation errors responses
Browse files Browse the repository at this point in the history
  • Loading branch information
santib committed Jan 6, 2021
1 parent 7f0bbed commit e83d7dc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions actiontext/test/dummy/app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create
if @message.save
redirect_to @message, notice: 'Message was successfully created.'
else
render :new
render :new, status: :unprocessable_entity
end
end

Expand All @@ -35,7 +35,7 @@ def update
if @message.update(message_params)
redirect_to @message, notice: 'Message was successfully updated.'
else
render :edit
render :edit, status: :unprocessable_entity
end
end

Expand Down
2 changes: 1 addition & 1 deletion guides/source/debugging_rails_applications.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class ArticlesController < ApplicationController
logger.debug "The article was saved and now the user is going to be redirected..."
redirect_to @article, notice: 'Article was successfully created.'
else
render :new
render :new, status: :unprocessable_entity
end
end
Expand Down
26 changes: 14 additions & 12 deletions guides/source/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ class ArticlesController < ApplicationController
if @article.save
redirect_to @article
else
render :new
render :new, status: :unprocessable_entity
end
end
end
Expand All @@ -814,7 +814,8 @@ will render `app/views/articles/new.html.erb`, which we will create next.
The `create` action instantiates a new article with values for the title and
body, and attempts to save it. If the article is saved successfully, the action
redirects the browser to the article's page at `"http://localhost:3000/articles/#{@article.id}"`.
Else, the action redisplays the form by rendering `app/views/articles/new.html.erb`.
Else, the action redisplays the form by rendering `app/views/articles/new.html.erb`
with a status code 4XX for the app to work fine with [Turbo](https://github.com/hotwired/turbo-rails).
The title and body here are dummy values. After we create the form, we will come
back and change these.

Expand Down Expand Up @@ -926,7 +927,7 @@ class ArticlesController < ApplicationController
if @article.save
redirect_to @article
else
render :new
render :new, status: :unprocessable_entity
end
end

Expand Down Expand Up @@ -1017,7 +1018,7 @@ To understand how all of this works together, let's take another look at the
if @article.save
redirect_to @article
else
render :new
render :new, status: :unprocessable_entity
end
end
```
Expand All @@ -1030,8 +1031,8 @@ messages.
When we submit the form, the `POST /articles` request is mapped to the `create`
action. The `create` action *does* attempt to save `@article`. Therefore,
validations *are* checked. If any validation fails, `@article` will not be
saved, and `app/views/articles/new.html.erb` will be rendered with error
messages.
saved, `app/views/articles/new.html.erb` will be rendered with error
messages with a status code 4XX for the app to work fine with [Turbo](https://github.com/hotwired/turbo-rails).

TIP: To learn more about validations, see [Active Record Validations](
active_record_validations.html). To learn more about validation error messages,
Expand Down Expand Up @@ -1090,7 +1091,7 @@ class ArticlesController < ApplicationController
if @article.save
redirect_to @article
else
render :new
render :new, status: :unprocessable_entity
end
end

Expand All @@ -1104,7 +1105,7 @@ class ArticlesController < ApplicationController
if @article.update(article_params)
redirect_to @article
else
render :edit
render :edit, status: :unprocessable_entity
end
end

Expand All @@ -1125,8 +1126,9 @@ action will render `app/views/articles/edit.html.erb`.
The `update` action (re-)fetches the article from the database, and attempts
to update it with the submitted form data filtered by `article_params`. If no
validations fail and the update is successful, the action redirects the browser
to the article's page. Else, the action redisplays the form, with error
messages, by rendering `app/views/articles/edit.html.erb`.
to the article's page. Else, the action redisplays the form with error
messages, by rendering `app/views/articles/edit.html.erb` with a status code 4XX
for the app to work fine with [Turbo](https://github.com/hotwired/turbo-rails).

#### Using Partials to Share View Code

Expand Down Expand Up @@ -1240,7 +1242,7 @@ class ArticlesController < ApplicationController
if @article.save
redirect_to @article
else
render :new
render :new, status: :unprocessable_entity
end
end

Expand All @@ -1254,7 +1256,7 @@ class ArticlesController < ApplicationController
if @article.update(article_params)
redirect_to @article
else
render :edit
render :edit, status: :unprocessable_entity
end
end

Expand Down
2 changes: 1 addition & 1 deletion guides/source/layouts_and_rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def update
if @book.update(book_params)
redirect_to(@book)
else
render :edit
render :edit, status: :unprocessable_entity
end
end
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class <%= controller_class_name %>Controller < ApplicationController
if @<%= orm_instance.save %>
redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully created.'" %>
else
render :new
render :new, status: :unprocessable_entity
end
end

Expand All @@ -40,7 +40,7 @@ class <%= controller_class_name %>Controller < ApplicationController
if @<%= orm_instance.update("#{singular_table_name}_params") %>
redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully updated.'" %>
else
render :edit
render :edit, status: :unprocessable_entity
end
end

Expand Down

0 comments on commit e83d7dc

Please sign in to comment.