How to embed dynamic Ruby code to javascript section in Slim templates?
what i prefer to do, is to keep all the javascript in a separate file.
for example, i would do it as follows(jquery):
in your layout:
...
<body data-product-ids="<%= existing_ids.to_json %>"
data-products-json="<%= @filter.data.to_json %>">
.. in js:
// create your application namespace
$.myapp = {};
$.myapp.allProductIds = $.parseJSON( $('body').attr('data-product-ids') );
$.myapp.products = $.parseJSON( $('body').attr('data-products-json') );
so, you'll then use it in js like so:
$.myapp.allProductIds
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 #{...}
.
What's the Best Way to Do Pre-formatted Code Snippets in Slim Templates
I've figured it out. I didn't realize multi-line strings could be nested under a single pipe like this:
pre
code
|
# The Greeter class
class Greeter
def initialize(name)
@name = name.capitalize
end
def salute
puts "Hello #{@name}!"
end
end
g = Greeter.new("world")
g.salute
slim template render title parameter
You need to define it as an instance variable so it will be available in the other templates.
- @myTitle = "title"
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
Related Topics
Why Does Rails Titlecase Add a Space to a Name
How to Get the File Extension from a Url
Rails: Update Model Attribute Without Invoking Callbacks
In a Sinatra App on Heroku, Session Is Not Shared Across Dynos
Error Installing Gem: Couldn't Reserve Space for Cygwin's Heap, Win32 Error 487
How to Store Enum as String to Database in Rails
Simple Neural Network Can't Learn Xor
How to Make a Non-Blocking Request for an Exclusive Lock Using File#Flock
Dealing with Large CSV Files (20G) in Ruby
Does Rails Come with a "Not Authorized" Exception
How to Create Temp Dir in Ruby
Sending a Delete Request from Sinatra
How to Return Correct Http Error Codes from Ruby on Rails Application
Accessing One Controller Variable in Another Controller in Rails