January 27 - Replicate the Rails image_tag Method in Ruby
This exercise asks you to replicate the functionality of the popular image_tag method provided by the Ruby on Rails framework.
Guide Tasks
- Read Tutorial
- Watch Guide Video
- Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license
Already a Bottega Student? Sign In
Summary
Build a program that replicates the functionality of the
image_tag
method provided by the Ruby on Rails framework.
Exercise Description
The method should be able to dynamically generate an HTML image tag. Specifically it needs to be able to take in any number of arguments and integrate the arguments and their values as elements contained in the generated image tag.
Examples
@image_path = "https://devcamp.com/some_pic.jpg" image_tag(@image_path)) # "<img src='https://devcamp.com/some_pic.jpg'>" image_tag(@image_path, width: 42)) # "<img src='https://devcamp.com/some_pic.jpg' width='42'>" image_tag(@image_path, alt: "My Image") # "<img src='https://devcamp.com/some_pic.jpg' width='42' alt='My Image'>"
Real World Usage
This process is utilized extensively when it comes to building view helper methods in Rails applications. If you work in Rails based applications you will encounter many situations when you need to dynamically generate HTML code.