Express中res.render和res.redirect的区别

xiaoxiao2021-02-28  49

render不会去执行controller中的action方法,直接渲染相应的页面文件,可以使用的。参数如下: [ruby] view plain copy render(:text => string) render(:inline => string, [:type => “rhtml”|”rxml”]) render(:action => action_name) render(:file => path, [:use_full_path => true|false]) render(:template => name) render(:partial => name) render(:nothing=>true) render() 第1行:直接渲染出文本 第2行:把传入的string渲染成模板(rhtml或者rxml) 第3行:直接调用某个action的模板,相当于forward到一个view 第4行:使用某个模板文件render, 当use_full_path参数为true时可以传入相对路径 第5行:使用模板名render,e.x.: render(:template => “blog/short_list”) 第6行:以局部模板渲染 第7行:什么也不输出,包括layout 第8行:默认的的render, 相当于render(:action => self) Redirect是跳转到一个新的action中继续执行,相当于浏览器发送了一个新的请求,并且默认返回302状态码。例如: [ruby] view plain copy def redirect_to(options = {}, response_status = {}) #:doc: raise ActionControllerError.new(“Cannot redirect to nil!”) if options.nil?

if options.is_a?(Hash) && options[:status] status = options.delete(:status) elsif response_status[:status] status = response_status[:status] else status = 302 end

response.redirected_to = options

case options

The scheme name consist of a letter followed by any combination of

letters, digits, and the plus (“+”), period (“.”), or hyphen (“-“)

characters; and is terminated by a colon (“:”).

when %r{^\w[\w\d+.-]:.} redirect_to_full_url(options, status) when String redirect_to_full_url(request.protocol + request.host_with_port + options, status) when :back if referer = request.headers[“Referer”] redirect_to(referer, :status=>status) else raise RedirectBackError end else redirect_to_full_url(url_for(options), status) end end

转载请注明原文地址: https://www.6miu.com/read-83943.html

最新回复(0)