Lisa Lisa - 2 years ago 99
Javascript Question

Accessing rails path from javascript

I'm trying to access the "next" method in my Rails controller from within a javascript file. From what I've read, it seems like I actually need to turn my js into a js.erb file, and reference the path with "asset_path." However, I keep getting an error message in my console saying "Invalid regular expression: missing /" that points to the line of code where I have:

$.get(<%= asset_path('next') %>, {id: projectId, next: 4}).done(function(response){

Basically, I'm just trying to access the method "def next" in one of my controllers. Is there an easier way?? Help!

Answer Source

Right you erb file is getting compiled to

$.get(/some/path/here, {id: projectId, next: 4}).done(function(response)

So to fix it wrap it in quotes

$.get("<%= asset_path('next') %>", {id: projectId, next: 4}).done(function(response)

which should get rendered to

$.get("/some/path/here", {id: projectId, next: 4}).done(function(response)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download