Inline ruby in :javascript haml tag?
You can use the string interpolation syntax (#{...}
) :
:javascript
var tab = #{@tab}
Take care of correctly escaping, however.
How can I use a Ruby variable in an inline JavaScript in haml?
You can use the simple "#{}" interpolation tags to use ruby variables with HAML.
Your code:
:javascript
alert(<%=raw @status %>)
Possible Solution:
:javascript
alert("#{@status}")
Ruby methods within Javascript within HAML
Usually, if something is a pain in haml, it means you should refactor the the tricky bit to a helper or partial and call that.
// some_helper.rb
def new_snazzy_select_tag(options = [])
select_tag 'tag_name_here', options.map { |option| [option.id, option.name] }
end
Also, you should use the :javascript
filter to render javascript since it will put it in a script tag for you and allow indentation.
Lastly, you can use #{ruby_expression}
anywhere in haml, including :javascript
filters, which is very handy when you need to output the result of ruby expressions to places that are not directly contents of html elements.
// some_view.html.haml
:javascript
$('#mylink').click(function() {
$('#mylink').after("#{escape_javascript new_snazzy_select_tag(myarray)}");
};
Inline Variables in JS Script for HAML
try this
- if @posting.pictures.blank?
- else
:javascript
$("#slider-posting").vegas({
slides: [
#{
@posting.pictures.map { |p| "{src: '#{p.url}'}"}.join(',')
}
],
transition: 'slideLeft'
});
How do I include inline JavaScript in Haml?
:javascript
$(document).ready( function() {
$('body').addClass( 'test' );
} );
Docs: http://haml.info/docs/yardoc/file.REFERENCE.html#javascript-filter
Is there a way to use a Ruby loop inside of HAML's :javascript region?
this one works
%script
- 10.upto(20) do |i|
document.getElementById('aDiv').innerHTML += '#{i}';
Injecting variable values into javascript and HAML in RoR
This should work ie. put all inline ruby inside of #{}
:
requester_name: "#{current_user.first_name + ' ' + current_user.last_name if current_user}",
requester_email: "#{current_user.email if current_user}",
Short way to include CSS and javascript tags with HAML
You don't need the script's type
attribute, and you can use the html syntax
%script(src="/js/myscript.js")
you could always create a "helper" to generate it if you feel like it
Javascript in HAML in Javascript
You have two options.
- You can use AJAX request to get data from database
You can use mapping concept like following
var data = @processed_data // json data, which contains processed data in json format.
var accu_id = $("#accu")[0].selectedOptions[0].value
var price = data.accu_id;
Here the @processed_data contains data like following
@processed_data = currency_calculation
def currency_calculation
Product.all.map { |product| [product.id, currency(product.price2) ] }.as_json
end
Example
Assume products table have two entries then the @processed_data contain values like following
{ "1" => "20.00", "2" => "25.50" }
The above data directly assigned to js variable data and accessed like data.key
The first option is best choice and second is possible one.
Note : You can't use js variable inside ruby.
Related Topics
Remove Duplicates Form an Array
How to Include Newlines in Labels in D3 Charts
What Is the Lifecycle of an Angularjs Controller
How to Include HTML in a Js Rails Response
Compare JavaScript Array of Objects to Get Min/Max
Jqgrid, How to Make a Column Editable in the Add Dialog But Not During (Inline) Edits
How to Search in Array of Object in Mongodb
Check If One Date Is Between Two Dates
Retrieve Dom Target from Drag Callback When 'This' Is Not Available
Resolve Circular References from JSON Object
What Is the "Hasclass" Function with Plain JavaScript
Pdf.Js: Rendering a PDF File Using a Base64 File Source Instead of Url
Vue.Js - How to Properly Watch for Nested Data
Selectionstart/Selectionend on Input Type="Number" No Longer Allowed in Chrome
Smart Way to Truncate Long Strings
How to Reduce JavaScript Object to Only Contain Properties from Interface
Angular: Can't Find Promise, Map, Set and Iterator
Angular 4.3.3 Httpclient:How Get Value from the Header of a Response