small medium large xlarge

02 Mar 2011, 07:27
John Peterson (2 posts)

This is probably a really simple installation issue. My browsers are displaying the generated .js files instead of executing them. For example, this view: `

Test Text

<%= button_to “Change”, :action => “change”, :remote => true, :format => :js %> ` And this script template: ` page.replace_html(“test”, “New Html”) ` produce the correct script, but the browser just displays it instead of executing. ` try { Element.update(“test”, “New Html”); } catch (e) { alert(‘RJS error:\n\n’ + e.toString()); alert(‘Element.update("test", "New Html");’); throw e } ` So it’s not a problem with the javascript libraries. The content type seems OK. Configuration issues?

02 Mar 2011, 15:55
Steve Castaneda (11 posts)

I was having this issue at one point, and putting script tags in the js file did the trick. You can also try removing the :format => :js, and moving that logic into the controller at the top:

respond_to :js, :html

Then in the particular method, in this case the “change” method:

def change
@variable = whatever

respond_with(@variable) do |format|
  format.html { redirect_to whereever_path }

This should cause the file to render the JS file correctly, which should be named change.js.erb and in the same directory as the view you’re rendering.

Does that do the trick?

02 Mar 2011, 22:29
John Peterson (2 posts)

I changed the rjs file to a .js.erb so that I could put the script tags in directly but no difference. I also looked at the traffic to make sure that the script had the right content type (Content-Type: text/javascript; charset=utf-8) and that looks good. So I don’t see anything on the server side that looks bad. So that would leave the javascript in the browser as the problem, but really all I have there is ` <%= button_to “Change”, :action => “change”, :remote => true, :format => :js %> ` Everything else is in the libraries. I have the ` <%= javascript_include_tag :defaults %> ` so that the javascript should be there as needed.

I had originally done this without the :format => :js but then it always rendered html so I had to force the .js.

Any further ideas?

03 Mar 2011, 01:42
Ed Jones (4 posts)

I don’t know, but I sure wish I did.

This is my second go round with this; I started in November and never did find an answer. There are ways to create a javascript file where you place somehting like:

n =$('add_new_user');"block"
n.innerHTML = "<%= escape_javascript(render :partial => "form") %>";

into a js.erb file.

Much of this week and last has been spent searching how to do this simple, simple task–with no results.

The docs insist that the rjs helpers still work, but no one seems to have documented how they work!

19 Mar 2012, 11:35
irene (1 post)

Hello! I’m just working on the manual to learn ruby and rails, but in this chapter I’ve got same problems than yours, but I can’t find the way to solve it. My Rails version is 3.2.1, is there any problem with this? I tried with the solutions given but it doesn’t work. Thank you very much.

PD: When analysing with firebug in Firefox, I find out that there is a “default.js” missing, ¿?

19 Mar 2012, 15:35
Robert W Karlsson (2 posts)

Ok I’m stuck on this problem as well. Using Rails 3.2.2 and the cart does not change until I refresh the page. Any help welcome.

You must be logged in to comment