Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS 

Redirect a URL with Ruby CGI

#!/usr/bin/ruby

require 'cgi'

cgi = CGI.new
print cgi.header({'Status' => '302 Moved', 'location' =>  'http://www.wired.com'})

or
url = 'http://www.wired.com/'
print cgi.header({'status'=>'REDIRECT', 'Location'=>url})


References:
RE: cgi redirect [nagaokaut.ac.jp]
Ruby/CGI - assari [mokehehe.com]
HTTP/1.1: Status Code Definitions [w3.org]

redirect a user to a new page

// description of your code here

// insert code here..

Rails: 301 Permanent Redirect

First there was:
 headers["Status"] = "301 Moved Permanently"  
 redirect_to "http://www.newdomain.com"  

Then there was:
head :moved_permanently, :location => book_url(@book)


As of [7820] on 2007/10/09, there's a new redirector in town.
  # Examples: 
  #   redirect_to post_url(@post), :status=>:found 
  #   redirect_to :action=>'atom', :status=>:moved_permanently 
  #   redirect_to post_url(@post), :status=>301 
  #   redirect_to :action=>'atom', :status=>302

sweet.

see: http://dev.rubyonrails.org/changeset/7820

Response::redirect //PHP Function

class Response{
	function redirect($url){
		exit(header('Location: ' . $url));
	}
}

HTML Redirect

I know this is a lame snippet, but I'm tired of writing this from scratch every time.

<html>
<head>
  <title>Page Title</title>
  <meta http-equiv="Refresh" content="2; URL=http://the.url">
  <script type="text/javascript">
    location.replace('http://the.url');
  </script>
</head>
<body>
  <p><a href="http://the.url">Page Title</a></p>
</body>
</html>

Java Script Redirect

<script language="JavaScript">
<!--
window.location="http://www.anotherpage.com/";
// -->
</script>


Alternately, something like..

<a href="#" onclick="window.location='http://www.something.com/';" return false;">Click here</a>


which stops it from being indexed (although rel="noindex" does the same on some engines).

Performing a redirect with AJAX

If a redirect is returned from the rails' form_remote_tag or link_to_remote methods, a message is displayed saying something like "You are being redirected", where "redirected" is a link the user must click. The code snippet below will actually redirect the user to the proper location, without them having to click the link. Code works for both form_remote_tag or link_to_remote methods.

<% link_to_remote "Foo",
                  :url => { :action => "foo" },
                  302 => "document.location = request.getResponseHeader('location')" %>



Or, an example from the Typo source:
<%= form_remote_tag :url => {:action => "comment", :id => @article}, 
                    :update => {:success => 'commentList'},
                    :loading => "loading()", 
                    :complete => "complete(request)",
                    :failure => "failure(request)",
                    :html => {:id=>"commentform",:class=>"commentform"},
	            302 => "document.location=request.getResponseHeader('location')"%>

Redirect back to a previous action

I am pasting this verbatim from LoginSystem

# move to the last store_location call or to the passed default one
  def redirect_back_or_default(default)
    if @session[:return_to].nil?
      redirect_to default
    else
      redirect_to_url @session[:return_to]
      @session[:return_to] = nil
    end
  end


In your controller...

 redirect_back_or_default :controller => "myfuel", :action => "index"

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS