{"id":19667238,"url":"https://github.com/willnet/ajax_error_renderer","last_synced_at":"2025-04-28T23:32:07.816Z","repository":{"id":59150499,"uuid":"159637635","full_name":"willnet/ajax_error_renderer","owner":"willnet","description":"a validation error renderer for ajax request","archived":false,"fork":false,"pushed_at":"2019-11-03T06:51:46.000Z","size":48,"stargazers_count":10,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-07T10:11:08.722Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willnet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-29T09:05:35.000Z","updated_at":"2020-11-06T01:45:49.000Z","dependencies_parsed_at":"2022-09-13T11:00:30.819Z","dependency_job_id":null,"html_url":"https://github.com/willnet/ajax_error_renderer","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnet%2Fajax_error_renderer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnet%2Fajax_error_renderer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnet%2Fajax_error_renderer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnet%2Fajax_error_renderer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willnet","download_url":"https://codeload.github.com/willnet/ajax_error_renderer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224137343,"owners_count":17262000,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T16:31:15.928Z","updated_at":"2024-11-11T16:31:49.368Z","avatar_url":"https://github.com/willnet.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AjaxErrorRenderer\n\n[![Gem Version](https://badge.fury.io/rb/ajax_error_renderer.svg)](https://badge.fury.io/rb/ajax_error_renderer) [![CircleCI](https://circleci.com/gh/willnet/ajax_error_renderer/tree/master.svg?style=svg)](https://circleci.com/gh/willnet/ajax_error_renderer/tree/master)\n\nAjaxErrorRenderer is a validation error renderer for ajax request.\n\nIf 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.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'ajax_error_renderer'\n```\n\nAnd then execute:\n\n```bash\n$ bundle\n```\n\n## Usage\n\nFirst, `include AjaxErrorRenderer` in your controller like following.\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  include AjaxErrorRenderer\nend\n```\n\nThen you can use render_ajax_error method like following.\n\n```ruby\nclass UsersController \u003c ApplicationController\n  def new\n    @user = User.new\n  end\n\n  def create\n    @user = User.new(params.require(:user).permit(:name))\n    if @user.save\n      redirect_to users_path, notice: 'You created a user successfully!'\n    else\n      render_ajax_error model: @user\n    end\n  end\nend\n```\n\nSo error messages display within `#error` by default.\n\n```erb\n\u003c%= form_with(model: @user) do |form| %\u003e\n  \u003cdiv id=\"error\"\u003e\n    \u003c%# Error Messages display here! %\u003e\n  \u003c/div\u003e\n\n  \u003cdiv class=\"field\"\u003e\n    \u003c%= form.label :name %\u003e\n    \u003c%= form.text_field :name %\u003e\n  \u003c/div\u003e\n\n  \u003cdiv class=\"actions\"\u003e\n    \u003c%= form.submit %\u003e\n  \u003c/div\u003e\n\u003c% end %\u003e\n```\n\nIf 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.\n\n\n```ruby\nrender_ajax_error model: @user, location: '.error_messages'\n```\n\n### Scroll to error messages\n\nIf you have a large form, users can miss validation error messages because the messages display above user screen. To solve the problem, render_ajax_error method has move option to scroll to error messages. It defaults to true.\n\nIf you won't scroll, you can write like following.\n\n```ruby\nrender_ajax_error model: @user, move: false\n```\n\n### Custom Template\n\nYou can create custome template with a generator like following.\n\n```sh\n./bin/rails g ajax_error_renderer:view\n```\n\nThis generator command generates `app/views/ajax_errors/_error.html.erb`, and you can customize it freely.\n\nTo use another templates, you can use `template` argument.\n\n```ruby\nrender_ajax_error model: @user, template: 'critical_error'\n```\n\nTemplate `app/views/ajax_errors/_critical_error.html.erb` will use.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillnet%2Fajax_error_renderer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillnet%2Fajax_error_renderer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillnet%2Fajax_error_renderer/lists"}