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

 ̄綄美尐妖づ 提交于 2019-12-09 16:15:58

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/38625276/rails-5-throw-abort-how-do-i-setup-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!