2

I have a Rails 3 application that is serving a js file e.g., tools.js.erb via the assets pipeline. This is working fine, but I want to add some html templates which are available as partials within the environment.

One option was to load the template via an ajax call from the JS when the template is required, but that would add some delay in presenting it on the client.

Is it possible to render a partial inside the JS and store it in a JS variable as a string, so I can just inject the html right away. What is the best way to do this?

3coins
  • 1,322
  • 3
  • 11
  • 30

3 Answers3

0

Yeah, you can do something like this:

var container = $("#your_container");
var myPartial = "<%= j render( :partial => 'directory/your_partial' ) %>";

// on.click, or whatever...
container.prepend(myPartial);

Not sure if you want to prepend or replace the HTML, but this should get you on the right track. Good call saving the markup for the partial in a Javascript variable. Should speed things up.

NM Pennypacker
  • 6,704
  • 11
  • 36
  • 38
  • Nick, thanks for replying. I am getting this. throw Error("NoMethodError: undefined method `render' for #<#:0x000039cfb2c740>\n – 3coins Dec 16 '14 at 20:49
  • I missed a semicolon after the variable declaration. – NM Pennypacker Dec 16 '14 at 21:01
  • I am using something like this. const test = "<%= j render(:partial => 'test', :locals => {:problem_types => Rails.application.config.problem_types}) %>"; But it doesn't seem to be working, throwing the error that I added before. – 3coins Dec 16 '14 at 21:04
  • Try making it a var. If that doesn't work, I don't know. It works for me in one of my apps. – NM Pennypacker Dec 16 '14 at 21:06
  • Thanks Nick, I don't think const is the issue, I tried var as well but it did not change anything. I believe your implementation is correct, but I am missing something silly that I am not able to point out. – 3coins Dec 16 '14 at 21:51
  • I think, I did find the root cause of the problem as noted in this post. http://stackoverflow.com/questions/8370399/rails-js-erb-file-cannot-find-method-render render cannot be used inside of assets folder. – 3coins Dec 16 '14 at 22:23
0

It HTML you want to add/update requires some variables, you would have to make a server trip anyways and would have reload your dom. Otherwise if it is a static content, you can follow this approach. Just create one variable in your action like this.

@my_view_content = render_to_string partial: 'path_to_partial', formats: ['html'], layout: false, object: something

Now, you can use this variable or pass this variable to your JS file.

var my_view_content_string = = "#{@my_view_content}"
mdev
  • 1,366
  • 17
  • 23
0

Why do you want to store a view in a variable if you have few good options..why cant you use a conditon..such as in show.js.erb

<% if condition %>

  $('#div1').html("<%= escape_javascript(render(:partial => 'pages/show')) %>");

<%else%>

  $('#div1').html("<%= escape_javascript(render(:partial => 'users/show')) %>");

<%end%>

ELSE THIS IS OTHER WAY TO DO IT....

###check of a condition in your view and then add element and call ajax

   <%if current_user.sign_in_count == 1 && current_user.confirmed?  %>
            <%= hidden_field(:new_user, :first_login, :value => current_user.sign_in_count)%>
  <%end%>    



  <script type="text/javascript">
$(document).ready(function() {
 //check if element is present in the dom and then call ajax
  if ($('#new_user_first_login').length) {
        $.ajax({
          //CALL AJAX
        })

  }
  })//document ends​
 </script>
Milind
  • 4,535
  • 2
  • 26
  • 58