为什么这种救援语法有效?

问题描述:

好的,所以我有正在使用的应用程序的这种方法,并且可以在生产环境中使用.我的问题为什么行得通?这是新的Ruby语法吗?

Ok so I have this method of an application I am working with and it works in production. My question why does this work? Is this new Ruby syntax?

def edit
  load_elements(current_user) unless current_user.role?(:admin)

  respond_to do |format|
    format.json { render :json => @user }   
    format.xml  { render :xml => @user }
    format.html
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:json, :xml, :html)
end

rescue在方法中不需要绑定到显式的begin,这就是定义语法的方式.有关示例,请参见此处#19 上面的骗子.

rescues do not need to be tied to an explicit begin when they're in a method, that's just the way the syntax is defined. For examples, see #19 here and this SO question, as well as the dupe above.