Вопрос

I have a URL like http://myapp.myorg.com/MyApp/one/two and I want to extract the first part myapp.myorg.com/MyApp/ in the Rails code. Please help me in getting this URL correct.

I'm currently using $servlet_context for this purpose, but it does not give me the result.

My code snippet:

<%= link_to "My Link", "{@hostname}/#{$servlet_context}/three/four" %>.

This gives me a URL like:

myapp/org.jruby.rack.servlet.ServletRackContext@622209db/three/four

I'm expecting something like:

myapp/MyApp/three/four
Это было полезно?

Решение

You can probably get this by extracting the first part of your path by defining this in your controller:

def context_path(url)
  URI.join('/' + request.fullpath.split('/')[1], url)
end
helper_method :context_path

Then that's easy to incorporate into your URL:

<%= link_to "My Link", context_path('three/four') %>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top