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

  • Home
  • SEARCH
  • 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 7858725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:34:36+00:00 2026-06-02T21:34:36+00:00

I’m using this javascript in substitution of remote_action: $(document).ready(function() { $(‘#asset_category_id’).change(function() { $.ajax({ url

  • 0

I’m using this javascript in substitution of remote_action:

$(document).ready(function() {
    $('#asset_category_id').change(function() {
        $.ajax({
            url : '<%= escape_javascript(url_for :controller => :categories, :action => :update_subcategories) %>',
    data : 'category_id=' + this.value,
    success : function(data) {
        $('#subcategories_div').html(data);
        $('#subcategories_div').show();
            }
        })
    });
});

The url in the following line is not being formed correctly:

url : '<%= escape_javascript(url_for :controller => :categories, :action => :update_subcategories) %>'

It is being used literally:

http://localhost:3000/assets/%3C%=%20escape_javascript(url_for%20:controller%20=%3E%20:categories,%20:action%20=%3E%20:updates_subcategories)%20%%3E?category_id=156

How can I use url_for in jQuery? Is it possible?

If I use

url : '/categories/update_subcategories'

everything runs as expected.

EDIT:
I have tried the following things without luck.

Seems that I should use preprocessing (guides.rubyonrails.org/asset_pipeline.html#preprocessing) as noted by @Matzi and @Baldrick so my file should be main.js.erb. I have renamed this file in app/assets/javascripts. When I go to the assets/new view (where the file is needed) I get this error:

undefined method 'url_for' for #<#<Class:0x9853e4c>:0xab00c48>

I’ve tried to add these two lines to the assets_controller:

include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper

Then I get:

undefined local variable or method `controller' for #<AssetsController:0xa40aaec>

In this line of the view:

<%= form_for(@asset) do |f| %>

I have also tried to add these lines to the new.html.erb file:

<%= include ActionView::Helpers::UrlHelper %>
<%= include ActionView::Helpers::TagHelper %>

Then I get:

undefined method `include' for #<#<Class:0xab164d0>:0xab0183c>

As recommended by Rails 3: How to render ERb template in rake task? in a similar case. I have tried the following includes in the view and controller with the same results:

include Rails.application.routes.url_helpers # brings ActionDispatch::Routing::UrlFor
include ActionView::Helpers::TagHelper

Another thing that I’ve tried, recommended by @Matzi is moving main.js.erb from app/assets/javascripts/ to app/views/assets. But in that case the Javascript in main.js.erb seems not to be loaded at the same time that the view new.html.erb. I’ve tried to rename main.js.erb to new.js.erb but the JS in the file seems not to be loaded neither.

I have though in using a JS to attach the handler and a js.erb to return the result but I need the url_for in the first JS in order to know where to call.

So the question remains, how can I use url_for in a js.erb that tries to attach a change handler to a field in a view?

  • 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-06-02T21:34:38+00:00Added an answer on June 2, 2026 at 9:34 pm

    I think the problem is that it is in a .js file, which is not interpreted by ruby. Try to put this code in a .js.erb file, and it will work for you.

    UPDATE:

    Asset is not for this kind of dynamic behavior, so you would better move it into some view, and render it, when needed. If you want to use it in an asset, you need to include the helpers in the .js.erb:

    <% environment.context_class.instance_eval { include ActionView::Helpers::UrlHelper } %>
    <% environment.context_class.instance_eval { include ActionView::Helpers::TagHelper } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

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.