I am a Ruby on Rails developer with over two years of experience, most of which is working remotely. I am a typical Punekar and in my free time I play badminton, workout, go trekking and I like to interact with people. Feel free to slide into my DMs and say Hi! 😄
1 minute read
One of the basic things we learn as a Rails developer is
how to interpolate bits and pieces of Ruby code into our template file, which is ERB.
There are many other template engines apart from ERB, such as HAML and Slim,
but since ERB is the default for Rails,
the examples shown below use the
Let us consider the example of displaying an image of my pet dog 🐶. Assume that we have the following attributes in a hash.
The above hash values can be added to HTML img tag as below:
We can also use
methods to replace HTML tags with Ruby code.
Here we define all the img tag attributes using a hash.
Rails 7 has
ability to transform a Ruby hash into HTML attributes for ERB interpolation by using the ActionView::Helpers::TagHelper#tag_options implementation.
This will allow us to mix Ruby hash into HTML tags along with other
HTML attributes to achieve the same result in the ERB.
Suppose we want to add an inline style to an HTML element and we prefer to do it using normal HTML attributes.
With this change, we can add inline style using the normal HTML style attribute.