Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:43:07+00:00 2026-05-24T09:43:07+00:00

using Rails 3.1.0.rc4, I’m trying to access a route helper in a javascript file

  • 0

using Rails 3.1.0.rc4, I’m trying to access a route helper in a javascript file (event.js.erb in this case) and it seems like they are not loaded at that moment. When requesting the merged /assets/application.js file, I get:

throw Error("NameError: undefined local variable or method `events_path' for #<#<Class:0x00000001580010>:0x00000003191510>\n  (in /<...>/app/assets/javascripts/event.js.erb)")

Any idea how to access the route helper in there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:43:09+00:00Added an answer on May 24, 2026 at 9:43 am

    UPDATE: Now there is a gem that does this for you: js-routes


    The problem is that Sprockets is evaluating the ERB outside of the context of your Rails app, and most of the stuff you’re expecting isn’t there.

    You can add things to your Sprockets context like so:

    Rails.application.assets.context_class.class_eval do
      include Rails.application.routes.url_helpers
    end
    

    That’s all well and good, but getting the helpers that require an id to work is a little trickier. I’m going to use a technique called a URI Template:

    var event_path = "<%= CGI.unescape event_path('{event_id}') %>";
    

    which returns /events/{event_id} which you could render into a url using an object like { event_id: 1 }. See SugarJS’s String#assign method as example implementation of this. You could also roll your own like I did.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Rails 3: In my update.js.erb file I found I was repeating a lot
I'm using rails, but doing all my Javascript unobtrusively using jQuery in application.js .
I am using rails beta 3 and I have a erb page named index.html.erb
Using Rails 3.2.0 with haml, sass and coffeescript: Basically I am trying to disable
Using Rails 3.2 and ruby 1.9.3p0 I am trying out the gem delayed_job. I
Using Rails 3, is there a way to use link_to helper, or any helper
Using Rails I'm trying to get an error message like The song field can't
I'm using Rails 2.3.2, and trying to get a nested object form to work
I'm using Rails 2.3.4/Ruby 1.9.1 for my application. In the view I have this
I am using rails 2.3.3 and ruby 1.9.1. I am trying to render a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.