Relative Path plugin

Plugin details

This plugin enables you to deploy your rails application behind reverse proxy which does not rewrite link url appropriately.

The concept is very simple. The return value of 'url_for' is a relative path when your controller includes RelativePath.

Repositoryhttp://opensvn.csie.org/relative_path/trunk Tags url, path LicenseUnknown

Documentation

Install the plugin:
ruby script/plugin install http://opensvn.csie.org/relative_path/trunk

Relative Path plugin

usage 1 :

class YourController < ApplicationController
 include RelativePath
end


usage 2 :

class ApplicationController
 include RelativePath
end


An example to use Static File Filter is below:

RelativePath.register_filter /\.css/,%r(src="(\.\./themes.+?)") do |env,match|
  referer = env["HTTP_REFERER"]
  if (req_uri = env["CLIENT_REQUEST_URI"]) && referer then
    referer = URI(referer)
    rel_path = req_uri + match[1] - referer
    "src=\"#{rel_path}\""
  else
    match[0]
  end
end


First argument /\.css/ is a regular expression to specify files you want to proccess.

This filter replace strings matched by the second argument with block's evaluated value.

Block's first argument *env* is a hash. The content of env is almost equal to HTTP Request header fields except that it is added CLIENT_REQUEST_URI.

env["CLIENT_REQUEST_URI"] contains an induced uri that is on user's browser. Keep it in mind that the inducing method is imperfect.

CLIENT_REQUEST_URI is useful when your CSS needs relative path from a user's browsing uri.

Block's second argument *match* is MatchData object that is matched by the regular expression of the second argument of register_filter method.

Because the matched string is replaced by the block's evaluated value, you have to return match[0] when you don't want to change anything.

An example to use Togglable Relative Path feature below:

<% with_relative_path_disabled do %>
   < td><%= link_to 'Show', :action => 'show', :id => user_id, :only_path => false %>< /td>
<%   with_relative_path_enabled do %>
   < td><%= link_to 'Edit', :action => 'edit', :id = user_id %>< /td>
<%   end %>
   < td><%= link_to 'Destroy', { :action => 'destroy', :id => user_id }, :confirm => 'Are you sure?', :method => :post %>< /td>
<% end %>

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | Back in time (1 older version) | Last edited by: hardway, 10 months ago