Skip to content

Latest commit

 

History

History
49 lines (40 loc) · 1.19 KB

README.md

File metadata and controls

49 lines (40 loc) · 1.19 KB

AjaxErrorRender

Formの送信をAjaxで通信で行った際、エラーメッセージを返すメソッドの提供をします。 このメソッドでは、#errorのHTML要素のinnerHTMLにエラーメッセージを挿入します。

Installation

Add this line to your application's Gemfile:

gem 'ajax_error_render'
$ bundle install

Or install it yourself as:

$ gem install ajax_error_render

Usage

  1. viewファイルの作成
    作成されるviewファイルは編集してカスタマイズすることができます。
$ rails generate ajax_error_render:install
  1. 使用するコントローラーでモジュールをincludeします。
    render_ajax_errorメソッドをモデルのインスタンスを引数に使用します。
    例)
require 'ajax_error_render'

class UsersController < ApplicationController
  include AjaxErrorRender

  def create
    @user = User.new(name: params[:user][:name])
    if @user.save
      redirect_to users_path
    else
      render_ajax_error(@user)
    end
  end
end

License

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