a validation error renderer for ajax request
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
app/views
bin
lib
test
.gitignore
Gemfile
MIT-LICENSE
README.md
Rakefile
ajax_error_renderer.gemspec

README.md

AjaxErrorRenderer

Gem Version

CircleCI

AjaxErrorRenderer is a validation error renderer for ajax request.

If you use turbolinks and form_with, you may need SJR(Server-generated JavaScript Responses) on validation errors. But it's a pain to add similar SJR code with every forms. AjaxErrorRenderer gives you render_ajax_error method to remove duplicated SJR.

Installation

Add this line to your application's Gemfile:

gem 'ajax_error_renderer'

And then execute:

$ bundle

Usage

First, include AjaxErrorRenderer in your controller like following.

class ApplicationController < ActionController::Base
  include AjaxErrorRenderer
end

Then you can use render_ajax_error method like following.

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(params.require(:user).permit(:name))
    if @user.save
      redirect_to users_path, notice: 'You created a user successfully!'
    else
      render_ajax_error model: @user
    end
  end
end

So error messages display within #error by default.

<%= form_with(model: @user) do |form| %>
  <div id="error">
    <%# Error Messages display here! %>
  </div>

  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

If you want to display the messages in other place, you can do it by passing location option to render_ajax_error with css like following.

render_ajax_error model: @user, location: '.error_messages'

Custom Template

You can create custome template with a generator like following.

./bin/rails g ajax_error_renderer:view

This generator command generates app/views/ajax_errors/_error.html.erb, and you can customize it freely.

License

The gem is available as open source under the terms of the MIT License.