简体   繁体   中英

I'd like to run ERB on a coffeescript file. Apparently, this can be done if placed on a view, but where exactly do I place the file?

There's some ERB code I'd like to run on a coffeescript file. Currently I have a controller named "places", and I'm working something on its index.

I have an index.html.haml on it, and I've managed to get coffeescript going from the assets pipeline (named places.js.coffee), but I haven't found a way to make ERB code to run from it. I read that it is possible to do this if the coffeescript is in your views. I've tried to place a coffeescript file named "index.js.coffee" inside the controller's view, but it doesn't work.

The file I tried to created is: /app/views/places/index.js.coffee

Am I using the wrong name? And/If-not, where should I place the file?

I'm on rails 4.0.2 with the coffee-rails gem (4.0.1).

Thanks in advance.

EDIT:

What I'm trying to accomplish is to use erb to inject code into the js/coffee, specifically - a set of arrays which will be used to fill data in a chart.

Normally, I'd have something like this in the places.js.coffee assets file:

labelsvariable = gon.labels

jQuery ->
data = {
    labels : labelsvariable,
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,1)",
            pointColor : "rgba(220,220,220,1)",
            pointStrokeColor : "#fff",
            data : [ 65,59,90,81,56,55,40 ]
        },
        ...
    ]
}

That works, I can use some data for the chart, but then I need to do more complex things, such as populating the datasets.

I know I could replace many of those with variables themselves, just like 'labelsvariable', but my problem comes when I bring a set of arrays that is to be included where the second data group is in the JQuery. I figured I could make a loop with erb, that could go through the array, replacing the whole datasets section, dumping the appropriate code for js/coffee.

I realize I may be taking the wrong approach, but I just couldn't come up with a better idea to send the data there. Any suggestions on that regard would be appreciated.

In regards to my original question, is it possible to run erb like this in a coffee file? If so, where should the coffee-file-that-also-runs-erb be placed?

Forgive me if I sound too vague, I'm having a really hard time to explain this. Thanks.

You can't render both index.html.erb/haml and index.js.coffee at the same time. Like the other answer mentioned, you'd typically render the .erb file on a normal page load, and the .js file to run Javascript on an already-loaded page as the response to an AJAX request.

I'm not sure I 100% understand what you're trying to achieve, but could you output the data into your HTML somewhere then pull it from the HTML with jQuery? Something like this:

In index.html.erb :

<div id="chart" data-datasets="<%= @data.to_json %>">
  Blah blah blah.
</div>

Then in a .js.coffee file that's in your assets pipeline (ie somewhere under app/assets/javascripts ):

$ ->
  datasets = $("#chart").data("datasets")

  data = {
    labels : labelsvariable,
    datasets : datasets
  }

I don't know where you're pulling the datasets data from but you might need to create a custom method (ie, something other than to_json ) that will output the data in a format that the javascript will understand.

In the HTML you don't necessarily have to put the data-datasets attribute on a <div> tag, I'd put it somewhere that's relevant to the part of the page the data gets used in.

The idea that you have to load it from your view is from the Rails documentation (I've lost it, sorry) ( Error using erb in js.coffee files in the rails asset pipeline ) which states that only assets should be placed in the asset pipeline

The way to load logic / conditional ( erb ) code into your JS is to include those files in the views, as that's the only place where the logic will work

This leaves your problem


Views

You'd get a better answer if you detailed what you want to do

I'll explain how you can call the JS from your views. The only way is when you call the page with Ajax (it sends the JS mime-type), like this:

#app/controllers/your_controller.rb
def action
   respond_to do |format|
       format.html
       format.js # loads action.js.erb IF you send a JS (ajax) request type
   end
end

If you provide more information on what you're trying to do, I can give you some specific ideas on how to implement!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM