[Nitro] Calculating action path

Lars Olsson lasso at lassoweb.se
Sun Sep 10 11:22:55 EDT 2006


When using self-referencing pages. When using CGI-style key-value
everything works fine, but not with "pretty urls":

# Using http://server/url?key1=val1&key2=val2 syntax. Works as intended.
def path1
   x = request['x'] || rand(100)
   new_x = rand(100)
   %|<p>This is #{x}</p>\n<p><a
href="#{request.path}?x=#{new_x}">Click</a></p>|
end

# Using http://server/url/param1/param2 syntax.
# Doesn't work since the action parameters is part of the uri
# Throws an argument error after 2nd click :(
def path2(x = rand(100))
   new_x = rand(100)
   %|<p>This is #{x}</p>\n<p><a
href="#{request.path}/#{new_x}">Click</a></p>|
end

Using the previously provided request.action_path method instead of
request.path works in both cases. (Unless when run on mongrel that
is...I get errors when running the action_path code on mongrel, but both
Webrick and lighthttpd work. Very strange...)


Sincerely

/lasso

________________________________________
Lars Olsson
lasso at lassoweb.nu
http://www.lassoweb.nu/



George Moschovitis skrev:
> I am courious, where is this needed?
> 
> -g.
> 
> PS: I am also not sure if you can already get this with the current code.
> 
> 
> On 9/9/06, Lars Olsson <lasso at lassoweb.se> wrote:
>> Hi list,
>>
>> I don't know whether there already exists functionality for calculating
>> the action path (if so, please tell me where!), bur since I need this
>> functionality in certain scenarios i added the following code to the
>> request object. Perhaps someone else might find it useful too.
>>
>> module Nitro
>>    module Request
>>      def action_path
>>        if uri
>>          offset = uri.include?('?') ? 2 : 1
>>          uri[0..uri.length - query_string.length - offset].chomp('/')
>>        else
>>          ''
>>        end
>>      end
>>    end
>> end
>>
>>
>> EXAMPLE
>> =======
>> Lets say we have an action that respond to http://server/somedir/myaction
>>
>> request.action_path will return "/somedir/myaction" in all the following
>> scenarios:
>>
>> http://server/somedir/myaction
>> http://server/somedir/myaction/
>> http://server/somedir/myaction?one=1&two=2&three=3
>> http://server/somedir/myaction/foo
>> http://server/somedir/myaction/foo?one=1&two=2&three=3
>> http://server/somedir/myaction/foo/bar
>> http://server/somedir/myaction/foo/bar?one=1&two=2&three=3
>> and so on...
>>
>> Improvements are most welcome.
>>
>> Sincerely
>>
>> /lasso
>>
>> --
>> ________________________________________
>> Lars Olsson
>> lasso at lassoweb.se
>> http://www.lassoweb.se/
>> _______________________________________________
>> Nitro-general mailing list
>> Nitro-general at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/nitro-general


More information about the Nitro-general mailing list