Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My problem is the following:

I want to use <%render => "xx"%> in a js.erb file - so I can't put it in my assets folder because render doesn't work in asset files.

I already tried to name it index.js.erb to get rails to include it automatically(it lies in the view folder, see below) but I guess that doesn't get included because I deleted the require_tree line in my admin.js(It's the "application.js" for my admin namespace). I can't use require tree simple because I don't want every js file in assets/javascripts/admin to be included.

I got my js file here:

views/admin/benefits/index.js.erb

I want to use it in the following view:

views/admin/benefits/index.html.erb

Am i overcomplicating something here? If not, how would I include it in my view? If it matters: I use rails 4.

share|improve this question
    
Did you get this solved? I've got the same problem. I'm trying to use render and moved the .js.erb file to my Views folder.. but it now seems like the file isn't included in the asset pipeline at all. –  Turgs Oct 12 '14 at 11:45

1 Answer 1

Move it to assets and add a optional yield to the head of your layout file.

In your layout file

<% if content_for?(:javascripts) %>
  yield(:javascripts)
<% end %>

In your view

<% content_for :javascripts do %>
  <%= javascript_include_tag 'benefits/index' %>
<% end %>

I think you are misunderstanding what the use of this file. js.erb files allow you to use Ruby in your JavaScript file. You can't actually render this file. You can look here to get a better understanding for why it is used.

share|improve this answer
    
My problem is not solved this way, I still can't use render. –  Evo_x Mar 25 '14 at 16:00
    
I updated my answer to explain what js.erb files are used for. –  CWitty Mar 25 '14 at 16:10
    
I don't want to render the js file. I want to render a partial in a js call, simple as that. I could put the whole js code directly in my view and it would work but since that not good style I want it in an external js file. –  Evo_x Mar 25 '14 at 16:12
    
Just have the js function return HTML from the js and insert it into the DOM. –  CWitty Mar 25 '14 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.