Inline Ruby in :JavaScript Haml Tag

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.

  1. You can use AJAX request to get data from database
  2. 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



Leave a reply



Submit