Friday, April 28, 2017

Creating a clickable image in Ruby on Rails

It is still my beginning stage to learn Ruby on Rails. I found that is quite challenging to create a clickable image in html.erb (Ruby Embedded HTML). A clickable image can be created easily by the <a href><img></a> pattern in plain HTML5.

For example, a very common pattern in plain HTML:
<a href="img/img-21-01.jpg">
  <img src="img/img-21-01.jpg" alt="Image" class="img-fluid tm-header-img">    
</a>
This pattern can create an image that can be clicked and enlarged.

After I searched Google, read some articles in StackOverflow and Michael Hartl's Tutorial book. I conclude a simplier <%= link_to image_tag %> pattern to create a clickable image:

<%= link_to image_tag("picture01.jpg", alt: "Image Description"), image_path("picture01.jpg") %>

Again, in plain HTML:
<a href="img/img-21-01.jpg">
  <img src="img/img-21-01.jpg" alt="Image" class="img-fluid tm-header-img">    
</a>

I re-wrote into html.erb:
<%= link_to image_tag("img-21-01.jpg", :alt => "Image", :class => "img-fluid tm-header-img" ), image_path("img-21-01.jpg") %>

This is a simplified pattern to create a clickable image in html.erb (Ruby Embedded HTML).

The images in the home page:

Click the image to enlarge it:


References:
ruby on rails link_to an image
http://stackoverflow.com/questions/18484543/ruby-on-rails-link-to-an-image

image_path (ActionView::Helpers::AssetTagHelper) - APIdock
https://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_path

5.1.1 Site navigation
https://www.railstutorial.org/book/filling_in_the_layout#sec-adding_to_the_layout

2 comments:

  1. Thanks for sharing such Programming details about ruby on rails and It is the interseting programming topic and to get some important information.on Ruby on Rails Online Training Bangalore

    ReplyDelete
  2. Really Good blog post.provided a helpful information.I hope that you will post more updates like this Ruby on Rails Online Training Bangalore

    ReplyDelete

How to kill an abandoned process in Linux/Unix

I remembered it, then I forgot, then I remembered it, and then I forgot again. In case of a Linux/Unit process hang, I have to figure out ...