Rails 是不是执行完 render 就跳出 action 了?

gaicitadie · 2018年04月01日 · 最后由 gaicitadie 回复于 2018年04月01日 · 1664 次阅读

我想在某些条件下 render 输出文本内容然后结束 action,类似 php 的 die,必须在 render 前加 return 吗?请大侠不吝赐教

试试不就知道了

会往下执行的,除非有 return

huacnlee 回复

感谢大虾赐教,不过我定义了一个用于 before_action 调用的方法,貌似不用 return 也会退出

def only_post_method
    if request.method != 'POST'
        render plain: '只允许post提交'
    end
end

before_action :only_post_method, :only => [:create, :update]

多查查文档吧 If a "before" filter renders or redirects, the action will not run. If there are additional filters scheduled to run after that filter, they are also cancelled.

http://guides.rubyonrails.org/action_controller_overview.html#filters

zouyu 回复

感谢大虾指点迷津🙏,看来我在文档阅读上还有欠缺

需要 登录 后方可回复, 如果你还没有账号请 注册新账号