views:

17

answers:

2

Hello, I was wondering if there's anyway to get a 'dynamic path' into a .js file through Ruby on Rails.

For example, I have the following:

new Ajax.Request('/tokens/destroy/' + GRID_ID, {asynchronous:true, evalScripts:true, onComplete:function(request){load('26', 'table1', request.responseText)}, parameters:'token=' + dsrc.id + '&authenticity_token=' + encodeURIComponent(AUTH_TOKEN)})

The main URL is '/tokens/destroy/:id', however on my production server this app runs as a sub folder. So the URL for this ajax call needs to be '/qrpsdrail/tokens/destroy/:id'

The URL this is being called from would be /grids/1 or /qrpsdrail/grids/1

I could, of course, do ../../path -- but that seems a bit hackish. It is also dependent on the routing never changing, which at this stage I can't guarantee. I'm just interested in seeing what other solutions there might be to this problem.

Thanks in advance :)

A: 

you can use Dynamic path in new.AjaxRequest using javascript in rails

javascript

 function dynamic_ajax(GRID_ID)
  {
      new Ajax.Request("/tokens/destroy?"+GRID_ID, {asynchronous:true, evalScripts:true, onComplete:function(request){load('26', 'table1', request.responseText)}, parameters:'token=' + dsrc.id + '&authenticity_token=' + encodeURIComponent(AUTH_TOKEN)});
  }

html

<a href="javascript:void(0)" onclick="dynamic_ajax('1')">Grid Id 1 </a>
<a href="javascript:void(0)" onclick="dynamic_ajax('2')">Grid Id 2 </a>
<a href="javascript:void(0)" onclick="dynamic_ajax('3')">Grid Id 3 </a>
Salil
The problem is not the GRID_ID that is a constant that I actually initialize elsewhere in the application - the problem is the URL that Ajax.Request calls - it doesn't work on both my environments because it calls absolute paths. Sorry if the question was ambiguous.
Robbie
+1  A: 

Maybe a bit hackish solution, but i have a configuration-file like described here, and so you could do something like, inside a config.yml :

development:
  root: /

production:
  root: /qrpsdrail/

and when you build your Ajaxrequest, you could write

new Ajax.Request("#{AppConfig.root}tokens/destroy/' + ...

But it still looks like there should be a cleaner way to solve this ;)

nathanvda
I kinda did something similar to this, but yeah I still think it's "hackish" ... I just set var ROOT_PATH = #{root_path} at the top of the template and can use it in the static grid.js library.I'm looking into perhaps using a javascript template (rjs?) ... but from what I've read that doesn't get cached like a .js file does.
Robbie