Ruby Slim – How do you define an element’s class with a rails helper or variable?
How about div[class=”sample #{@variable.name}”] or even div class=[“sample”, @variable.name] or .sample *{:class => [@variable1.name, @variable2.name]}
How about div[class=”sample #{@variable.name}”] or even div class=[“sample”, @variable.name] or .sample *{:class => [@variable1.name, @variable2.name]}
You can use a style similar to string interpolation. See example below. javascript: var config = { custom: “#{my_value ? ‘truthy’ : ‘falsy’}”, current_user: #{raw current_user.to_json} }; ** Update below ** If you want more advanced configuration I would recommend to create a class, for example class ClientConfig attr_accessor :foo, :bar # .. code def … Read more
In Slim javascript: var all_product_ids = “#{existing_ids.to_json}”; var products_json = “#{@filter.data.to_json}”;
This helped me. div :class=”[‘obj-‘ + obj.id]” style=”float:right; color:red;”
What’s happening is that “#{@user_name}” is being interpreted as CoffeeScript, not as Ruby code that’s evaluated and injected into the CoffeeScript source. You’re asking, “How do I inject a Ruby variable into my CoffeeScript source?” The short answer is: Don’t do this. The Rails team made an intentional decision not to support embedded CoffeeScript in … Read more
There are multiple ways in Slim As Hash Attributes which will be hyphenated if a Hash is given (e.g. data={a:1,b:2} will render as data-a=”1″ data-b=”2″) Use it directly as “mu is too short” mentioned, quite intuitive. a data-title=”help” data-content=”foo” Use Ruby code. I often do this and rarely above. = link_to ‘foo’, bar_path, data: {a: … Read more
See the examples below: div class=(is_active? ? ‘active’ : ‘inactive’) div class=(‘active’ if is_active?) The same approach can be used to assign dynamic values to other attributes.
Two big advantages of using slim over haml: Slim is currently about eight times faster than haml. Slim supports HTTP streaming, while HAML doesn’t. Slim has a more natural syntax: a href=”https://stackoverflow.com/questions/11390512/foo.html”