Rails 3: link_to and image_tag

How can I attach the image tag to the background.jpg file in my public / images and when I click on the user redirect to root_url (so the contents of the page are in the container, and if the user clicks on the background images are redirected to their homeland?) Thank you!

+8
ruby ruby-on-rails image hyperlink
source share
3 answers

Of course:

 <%= link_to(image_tag("background.jpg", :alt => "home image", :width => 100, :height => 100, :title => "Click here to return to Home") "/") %> 
+14
source share

Most rails tag helpers accept a block, which means you can use do to make your life easier. Here I use a block with link_to :

 <%= link_to root_url do %> <%= image_tag "background.jpg" %> <% end %> 

In this case, link_to expects the next parameter to be the path, and the block will emit what binds the binding to.

In general, I try to stick to one tag helper in the rule of each line, unless it is super trivial, so prefer this technique when the line is otherwise full. The advantage of this method is that the tags are on different lines, errors are easier to identify.

If you need other options, add them as usual. For example, I will add the css class:

 <%= link_to root_url, :class => 'imagelink' do %> ... 
+6
source share

I was looking for the same thing at the time, and I found a way for Rails to do this.

Suppose you want an HTML page to display the following code

 <a title="Return Home" class="logo" href="/pages/home"> <img width="200" height="50" src="logo.png" alt="Logo Image"> </a> 

Where "pages" is the controller and "home" is the action. Here's the Rails path.

  <%=link_to(image_tag("12roots-logo.png",:size => "209x50", :alt=> "12roots"), {:controller=>"pages", :action=>"home"}, :title=>"Return Home", :class=>"logo") %> 
+4
source share

All Articles