Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5 throw abort : how do I setup error messages?

Rails has introduced this throw(:abort) syntax, but now how do I get meaningful destroy errors ?

For validation errors one would do

if not user.save
  # => user.errors has information

if not user.destroy
  # => user.errors is empty

Here is my model

class User

  before_destroy :destroy_validation,
    if: :some_reason

  private

  def destroy_validation
    throw(:abort) if some_condition
  end
like image 439
Cyril Duchon-Doris Avatar asked Jul 28 '16 00:07

Cyril Duchon-Doris


2 Answers

You can use errors.add for your class method.

User model:

def destroy_validation
  if some_condition
    errors.add(:base, "can't be destroyed cause x,y or z")
    throw(:abort)
  end
end

Users controller:

def destroy
  if @user.destroy
    respond_to do |format|
      format.html { redirect_to users_path, notice: ':)' }
      format.json { head :no_content }
    end
  else
    respond_to do |format|
      format.html { redirect_to users_path, alert: ":( #{@user.errors[:base]}"}
    end
  end
end
like image 177
Gonzalo S Avatar answered Sep 16 '22 13:09

Gonzalo S


Gonzalo S answer is perfectly fine. Howerver if you want a bit more cleaner code you can considder a helper method. The following code works best in Rails 5.0 or higher since you can make use of the ApplicationRecord model.

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

private

  def halt(tag: :abort, attr: :base, msg: nil)
    errors.add(attr, msg) if msg
    throw(tag)
  end

end

Now you can do:

class User < ApplicationRecord

  before_destroy(if: :condition) { halt msg: 'Your message.' }

  # or if you have some longer condition:
  before_destroy if: -> { condition1 && condition2 && condition3 } do
    halt msg: 'Your message.'
  end

  # or more in lines with your example:
  before_destroy :destroy_validation, if: :some_reason
  
private

  def destroy_validation
    halt msg: 'Your message.' if some_condition
  end

end
like image 37
3limin4t0r Avatar answered Sep 17 '22 13:09

3limin4t0r