为什么这种救援语法有效? [英] Why does this rescue syntax work?

查看:81
本文介绍了为什么这种救援语法有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有正在使用的应用程序的这种方法,并且可以在生产环境中使用.我的问题为什么行得通?这是新的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.

这篇关于为什么这种救援语法有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆