古詩詞大全網 - 成語用法 - render和redirect的區別是什麽

render和redirect的區別是什麽

區別是:

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