Last active
November 1, 2024 17:28
-
-
Save ahmad19/58342e893412485f2f609f44ea3eec27 to your computer and use it in GitHub Desktop.
Render model validation errors inline by adding a simple presenter class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_with(model: post) do |form| %> | |
<% error_handler = ::InlineErrorHandler.new(post, self) %> # Initialize the error handler presenter here, or consider doing this in the controller | |
<div> | |
<%= form.label :title, style: "display: block" %> | |
<%= form.text_field :title %> | |
<%= error_handler.error_message_for(:title) %> # Display inline error for the title field here | |
</div> | |
<div> | |
<%= form.label :body, style: "display: block" %> | |
<%= form.text_area :body %> | |
<%= error_handler.error_message_for(:body) %> # Display inline error for the body field here | |
</div> | |
<div> | |
<%= form.submit %> | |
</div> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.error { | |
color: red; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InlineErrorHandler | |
def initialize(model, view_context) | |
@model = model | |
@view_context = view_context | |
end | |
def error_message_for(attribute) | |
if errors[attribute].present? | |
@view_context.content_tag(:span, errors[attribute].join(", "), class: "error") | |
end | |
end | |
private | |
attr_reader :model, :view_context | |
def errors | |
@errors ||= model.errors | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe InlineErrorHandler do | |
let(:model) { Post.new } | |
let(:view_context) { ApplicationController.helpers } | |
let(:inline_error_handler) { described_class.new(model, view_context) } | |
describe '#error_message_for' do | |
context 'when there is an error' do | |
before do | |
model.valid? | |
end | |
it 'returns the error message' do | |
expect(inline_error_handler.error_message_for(:title)).to eq("<span class=\"error\">can't be blank</span>") | |
end | |
end | |
context 'when there is no error' do | |
it 'returns nil' do | |
expect(inline_error_handler.error_message_for(:title)).to be_nil | |
end | |
end | |
end | |
end |
Author
ahmad19
commented
Nov 1, 2024

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment