Rewrite Template.Js.Erb into Template.Js.Slim

Rewrite template.js.erb into template.js.slim

| $('#new_reservation').hide().after('
= j render("new_reservation")
| ');

you can check this gem for further simple conversions

Possible to use ERB/slim template instead of EJS (JST) after Ajax update?

With your ejs.erb, Rails is generating the template, but has no idea what you will use it for. Then your Backbone app is fetching the tip from Rails via ajax, and populate the model accordingly. Then it's your backbone app that populates and renders the template. Rails cannot chime in here, at least not in this configuration where backbone expects json and then renders the template. Everything is happening in the front-end.

The only thing you can do is ask Rails to give you the result of your helpers inside the json. I am guessing you have a Tip.rb model and it should implement:

include ActionView::Helpers::DateHelper
...
def as_json(options = {})
{
:name => self.name,
... (all the model attributes that you want to expose)
:date_age => time_ago_in_words self.created_at
}
end

That being said, I would def use a client side solution, also because: http://rails-bestpractices.com/posts/105-not-use-time_ago_in_words

How to use slim templates with backbone?

Fat-free client-side templates with Slim and CoffeeScript: https://github.com/jfirebaugh/skim

Passing a string, to a javascript script, inside a slim template, in Rails

You have to quote the output from erb:

javascript:
var lineData = {
labels: #{raw @labels.to_json},
datasets: [{
label: '#{raw @legend}',
backgroundColor: 'rgba(114,102,186,0.2)',
borderColor: 'rgba(114,102,186,1)',
pointBorderColor: '#fff',
data: #{raw @data_set.to_json}
}

You could also just construct the entire hash in ruby and convert it into JSON:

var lineData = <%=
{
labels: @labels,
datasets: [{
label: @labels,
backgroundColor: 'rgba(114,102,186,0.2)',
borderColor: 'rgba(114,102,186,1)',
pointBorderColor: '#fff',
data: @data_set
}]
}.to_json
%>

How to use js.erb views in Rails 6

Create a view file next_question.js.erb, then do this

def next_question
@lesson = lesson

respond_to do |format|
format.js
end
end

Also, enable ujs in application.js file, add this line

require("@rails/ujs").start()

Give it a try!

Rails code for format js do not work

To get this to work you need to set layout to false and declare the content_type you're using.

From what I know/have read this is standard behaviour in rails. Here is a similar post discussing it with a link to a bug log. https://stackoverflow.com/a/1170121/3366016

Change your controller action to this:

  def new
@letter = UserSupportLetter.new
respond_to do |format|
format.js { render layout: false, content_type: 'text/javascript' }
format.html
end
end

By default it will try to load in the current layout for the given content type. For JS it typically is smart enough to know not to load a layout. By setting false you are disabling the layout and just telling it to render without a layout.

It seems if you are using some form of templateing it messes with the requests a bit. This answer seems to identify in the comments a specific work around for slim template. This answer describes another solution, basically using the default layout setup. With this, Rails seems to know how to handle it for JS requests.

how to embed the result of a Ruby variable in a slim Javscript block

According to the docs, it looks like you want:

javascript: 
window.ads.hasMap = #{show_map}

That is, wrap your ruby variable or method in #{...}.



Related Topics



Leave a reply



Submit