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

gaicitadie · April 01, 2018 · Last by gaicitadie replied at April 01, 2018 · 1665 hits

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

试试不就知道了

会往下执行的,除非有 return

Reply to 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

Reply to zouyu

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

You need to Sign in before reply, if you don't have an account, please Sign up first.