0

I'm running rails 4.0.4. I have a form I'm using a partial in to add a subset of fields. The user has the option to remove those fields and replace them with other fields once they're on the page. I'd like to be able to add those fields back in in the event that they want to undo their decision.

Render isn't available in javascript files (which seems odd for js.erb files). I've gotten as far as using the following to read the text of the file into a variable.

var master_quantity = "<%= data='';File.open("#{Rails.root}/app/views/shared/_a2a_master_quantity.html.erb","r").each_line{|x| data+=x};data %>";

Unfortunately escape_javascript doesn't appear to be available in the asests folder either, so I'm unable to use this string as it breaks the javascript.

Anyone have any suggestions on cleaning the string or alternate methods to get the partial into my javascript?

biagidp
  • 2,175
  • 3
  • 18
  • 29
  • I am not sure if this question is a duplicate, but these answers look relevant. [rendering a html.erb in rails with javascript](http://stackoverflow.com/questions/9617658/rendering-a-html-erb-in-rails-with-javascript) – dcorking Sep 27 '14 at 12:15

2 Answers2

1

You could try,

controller = ActionController::Base.new
controller.send(:render_to_string, '/shared/a2a_master_quantity')

Whatever you pass to render_to_string above are params for that method. You may or may not need that leading '/' for '/shared'.

Michael De Silva
  • 3,808
  • 1
  • 20
  • 24
  • This appears to have the same issue as opening a File object directly, which is that I can get the string but I still have no way to escape it. – biagidp Sep 29 '14 at 12:12
  • I found a post that explains how to use escape_javascript in an asset file such as javascript. By including ActionView::Helpers::JavaScriptHelper either this solution or my original solution via File.open work. http://stackoverflow.com/questions/6838550/how-do-i-get-escape-javascript-and-other-helpers-in-my-sprockets-pre-processed-j – biagidp Sep 29 '14 at 12:17
0

If I understand your question, and the partial never changes, you won't need to get it from the server. Just hide it in CSS (such as with JQuery's $('#selector').hide() )

On the other hand, if you need Ruby to customize your partial each time the user restores it, you can send an AJAX request from the browser to your server to request a new version of the partial.

Add a line to a suitable controller action that renders your partial:

render :partial => 'a2a_master_quantity' and return if request.xhr?

Make a suitable XML HTTP Request in your JavaScript to get this partial (such as JQuery's $.ajax().)

Then use JavaScript to add it to the DOM in the appropriate place.

dcorking
  • 1,146
  • 1
  • 14
  • 24
  • 1
    Because it has form fields in it I want them removed from the DOM so they never hit the back end. Hiding them and setting them to empty strings should be sufficient, it just irks me that there's not a way to do this. – biagidp Sep 26 '14 at 18:49
  • If I understand your comment correctly, you want to delete and add things from the document while the user is viewing the page. That can't be done in the browser with Ruby unless you use something like Opal, which is why there is no 'render' in ERB. ERB can't do it because it creates all your JavaScript before the files are sent to the browser. Instead, just read and write the `innerHTML` properties in JavaScript's DOM. But perhaps I don't properly understand your question. – dcorking Sep 27 '14 at 11:19
  • You seem to be understanding correctly. The page I'm on starts with an empty layout where you can add and remove elements. I'm trying to use the same partial to build the initial page and the html snippet in the js file to use when all elements have been removed. I was under the impression that a js.erb file interprets the embedded ruby first, then serves up the javascript file. I was just hoping to use a pre-existing ruby partial as a string to build the html in the javascript since it wont be changing on the client side. – biagidp Sep 29 '14 at 11:55
  • 1
    Now I think I understand. I think you have your approach working: pleased to hear it. One small disadvantage is that you send the HTML snippet to the browser twice: once in the page, and once in the script. I think you (and a maintenance programmer) may find it simpler to read the HTML snippet into your JavaScript variable (master_quantity) directly from the rendered page from an innerHTML property. – dcorking Sep 29 '14 at 17:27
  • 1
    Ahhh, I think I see what you're saying! I'd have to copy the element into the master_quantity variable on page load, since the page changes a lot once the user starts interacting with it, but that could be much more efficient than what I had to do on the server side to read the rails partial in the javascript. – biagidp Oct 01 '14 at 11:21
  • @biagidp Yes - you explain my idea well. Best wishes for whichever approach you put into production. – dcorking Oct 01 '14 at 11:31