1

I am a newbie developing using rails and jquery and having been searching everywhere for a simple example of how to do this for days. I have a page, poweroutput.html.erb, that shows the power output of a customer's solar power system for a particular date. When they input a different date, I want them to see different output, but without having to reload the page. On my page I have some javascript as well as a form (for submitting the date for which a user would like to see the power output) as follows:

<head>
  <script type="text/javascript">
    <%= render "pages/poweroutputscript" %>  # renders a partial containing all my js
  </script>
</head>

# a form is here on this page too

My page's controller action is currently as follows:

def poweroutput      
  # some code 

  if request.xhr?
    @date = params[:dt]         # dt is posted when form is submitted
  else 
    @date = Time.now.getlocal.strftime("%d.%m.%Y")
  end

  respond_to do |format|
    format.html 
    format.js { render :action "pages/poweroutput.js.erb" }
  end
end

And poweroutput.js.erb is as follows:

$("script").html("<%= escape_javascript render "pages/poweroutputscript" %>");

As is, the form submits successfully and I can see on Firebug that the js between my script tags is indeed being replaced with the new js. But the view in my browser is not updating accordingly. What am I doing wrong?

1 Answer 1

1

I don't think replacing the content of the <script> tag will cause it to run. Try this:

$("script").replaceWith("<script><%= escape_javascript render "pages/poweroutputscript" %></script>");

or just create a new <script> and add it to your document.

Sign up to request clarification or add additional context in comments.

2 Comments

YES! This worked! Thank you! I have been on the verge of tears about this for days.
Firebug does not recognize the <script> tags after replaceWith does its work. Do you know why this might be? I've just been running into some strange problems and am trying to rule things out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.