0

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
wurde
  • 2,487
  • 2
  • 20
  • 39
TipTop
  • 77
  • 1
  • 2
  • 9

1 Answers1

0

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') %>
Community
  • 1
  • 1
tadman
  • 208,517
  • 23
  • 234
  • 262
  • when I added this method in the controller, I get the error, method not found. Instead if add this in the model, then I can call this method in the name create_context_path(url). When I execute this code, it gives the error "request" not found. Also it expects create_context_path.erb file. Please help me. – TipTop Apr 29 '14 at 12:35
  • This is controller code that's also accessible in the view. You can't put it in a model, and in general models are not supposed to know anything about controllers. It breaks the concerns. – tadman Apr 29 '14 at 14:34
  • I have a helper file , I can put this function there. Its accessible from the partial. But there request is coming as nil. Any clues like whether I need to declare a request object in the controller? – TipTop Apr 29 '14 at 20:06
  • If you're using this in the controller or view then `request` will be defined. Inside a model that doesn't make any sense and won't work. – tadman Apr 29 '14 at 21:19