Module: Ramaze::Helper::Redirect
- Defined in:
- lib/ramaze/helper/redirect.rb
Overview
Helper::Redirect actually takes advantage of Helper::Link.link_raw to build the links it redirects to. It doesn’t do much else than this:
setting a status-code of 303 and a response['Location'] = link
returning some nice text for visitors who insist on ignoring those hints :P
Usage:
redirect Rs()
redirect R(MainController)
redirect R(MainController, :foo)
redirect 'foo/bar'
redirect 'foo/bar', :status => 301
TODO:
- maybe some more options, like a delay
Instance Method Summary collapse
-
#raw_redirect(target, opts = {}) ⇒ Object
Do not perform any mutations on the target like #redirect does.
-
#redirect(target, opts = {}) ⇒ Object
Usage: redirect Rs() redirect R(MainController) redirect R(MainController, :foo) redirect ‘foo/bar’ redirect ‘foo/bar’, :status => 301.
-
#redirect_referer ⇒ Object
(also: #redirect_referrer)
redirect to the location the browser says it’s coming from.
-
#redirected? ⇒ Boolean
Are we being redirected?.
-
#respond(*args) ⇒ Object
render to the browser directly, ignoring any templates.
Instance Method Details
#raw_redirect(target, opts = {}) ⇒ Object
Do not perform any mutations on the target like #redirect does. Suitable if you have to redirect to a different subdomain or host.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ramaze/helper/redirect.rb', line 59 def raw_redirect(target, opts = {}) target = target.to_s header = {'Location' => target} status = opts[:status] || STATUS_CODE["See Other"] body = %{You are being redirected, please follow <a href="#{target}">this link to: #{target}</a>!} Log.info("Redirect to '#{target}'") request[:redirected] = true throw(:redirect, [body, status, header]) end |
#redirect(target, opts = {}) ⇒ Object
Usage:
redirect Rs()
redirect R(MainController)
redirect R(MainController, :foo)
redirect 'foo/bar'
redirect 'foo/bar', :status => 301
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ramaze/helper/redirect.rb', line 44 def redirect(target, opts = {}) target = target.to_s unless target =~ %r!^https?://! target[0,0] = '/' unless target =~ %r!^/! if host = request.env['HTTP_HOST'] target[0,0] = "http://#{host}" end end raw_redirect(target, opts) end |
#redirect_referer ⇒ Object Also known as: redirect_referrer
redirect to the location the browser says it’s coming from.
77 78 79 |
# File 'lib/ramaze/helper/redirect.rb', line 77 def redirect_referer redirect request.referer end |
#redirected? ⇒ Boolean
Are we being redirected?
71 72 73 |
# File 'lib/ramaze/helper/redirect.rb', line 71 def redirected? request[:redirected] end |
#respond(*args) ⇒ Object
render to the browser directly, ignoring any templates
Usage:
respond 'Page not found', 404
respond render_template('forbidden.erb'), 403
respond File.open('file.jpg'), 200, 'Content-Type' => 'image/jpeg'
32 33 34 35 |
# File 'lib/ramaze/helper/redirect.rb', line 32 def respond(*args) response.build(*args) throw(:respond) end |