Ruby Slim - How to Define an Element's Class with a Rails Helper or Variable

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]}

Ruby slim - class for a div from variable

you could try:

.col-md-7 class="your-#{dynamic class}"

rails variable within div class name

You need to use an erb tag in your erb templates:

<div class="<%= my_class_name %>">

Forward slash in a Slim classname?

Slim doesn't support forward slashes in class names.
I think there is the only one way for it: div class="w-1/3"

Slim with embedded Ruby

A tag followed by text will output that text as the tag's contents. You can use string interpolation #{expression} inside the text to output variables.

Content text with a variable interpolated:

title #{yield(:title)} | Ruby on Rails Tutorial Sample App

You can also set the tag's contents to a ruby expression by using tag =.

title = yield(:title) + " | Ruby on Rails Tutorial Sample App"

Note that in this case this won't work if yield(:title) returns nil

When you have nested tags, you should put them on seperate lines.

slim dynamic conditional class

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.

Proper syntax for including a ruby variable in an Ajax call inside a SLIM view

Use this:

'/users_activities?email=#{@user.email}'

Slim Syntax for Inline Div/Table Classes with Embedded Ruby

The doc for this feature is here:

td class="#{@article.attribute}"

Slim: Append text to printed Ruby variable

Figured it out .. I just had to do:

a href = data.api.docs+"#some-subsection" Clicky

.. to generate:

<a href="https://docs.example.com/api#some-subsection">Clicky</a>


Related Topics



Leave a reply



Submit