F677fa685a2cfe8aff31f161062db3d3

I have two links side by side and they contain a lot of them same code, but im just not sure how to combine them.

<%= link_to image_tag("go_back.png"), user_path(@user) %><%= link_to 'Go Back', user_path(@user) %>

Refactorings

No refactoring yet !

4ebfc8d183472ac3cffff96be3e0ec68

PabloBM

August 6, 2008, August 06, 2008 09:54, permalink

No rating. Login to rate!

To be honest, I would probably leave it as is in my own code. However, if you want a DRY alternative, you can create the following helper, which you can then reuse for a "go_forward" link (if appropriate) or any other similar repeated links you may have.

def links_to(labels, options = {}, html_options = nil)
  labels.collect{|label| link_to(label, options, html_options)}.join
end
<%= links_to([image_tag("go_back.png"), 'Go Back'], user_path(@user)) %>
92772ff5353c89d9bd10f8e334161e16

Ben Hughes

August 6, 2008, August 06, 2008 11:53, permalink

No rating. Login to rate!

I tend to define helpers for common image-link pairs like this.

def back_link_to(name, options = {}, html_options = {})
  link_to(image_tag('go_back.png', options, html_options) + link_to(name, options, html_options)
end
D8daae35e495689a5f17d86f15d18eb4

rjspotter

August 6, 2008, August 06, 2008 19:31, permalink

1 rating. Login to rate!
<%= link_to "#{image_tag("go_back.png")}Go Back", user_path(@user) %>
D8daae35e495689a5f17d86f15d18eb4

rjspotter

August 6, 2008, August 06, 2008 19:45, permalink

No rating. Login to rate!

alternate suggestion

<style type="text/css">
  a.go_back { background: url(/images/go_back.png) center left no-repeat; padding-left: /*width of go_back.png*/;}
</style>

<%= link_to 'Go Back', user_path(@user), :class => 'go_back' %>
8484df61a1434e91cb088f53cc089f18

Adam

August 12, 2008, August 12, 2008 18:51, permalink

No rating. Login to rate!
<% link_to user_path(@user) do %>
	<%= image_tag("go_back.png") %>
	Go Back
<% end %>
035687df00d162cec025302373ebc076

chovy.myopenid.com

October 5, 2008, October 05, 2008 01:58, permalink

1 rating. Login to rate!

The image itself would be a 32px x 96px image with the 3 states (link, active, hover) shifted up 32px for each state. This allows the image to preload and there will be no delay when doing rollovers.

The text is indented offscreen -9999px so it is not visable when the browser supports CSS. If the browser has CSS disabled, and/or images disabled, the user would simply see the text.

The title attribute is added to the link tag to give users a tooltip and assistive devices.

<%= link_to "Go back", path_to_go_back, { :class => "prev", :title => "Previous page" } %>
<style type="text/css" media="screen">
// normal button icon
a.prev:link, a.prev:visited, a.prev:hover, a.prev:active {
    display: block; width: 32px; height: 32px; text-decoration: none; text-indent: -9999px;
    background: transparent url('/images/icons_prev.png') no-repeat 0 0;
}

// highlighted button
a.prev:hover {
    background-position: 0 -32px;
}

// depressed button icon
a.prev:active {
    background-position: 0 -64px;
}
</style>
30a45c64b2ef288f7ec098c5fb353a22

Nathanvda

May 6, 2009, May 06, 2009 11:22, permalink

No rating. Login to rate!

You can easily combine both the image and the text into one link. If that is the intention, that is. I do like the solution just before mine, just using css.

<%= link_to image_tag("go_back.png") + 'Go Back', user_path(@user) %>
B5987fd8e80364f29a2b93a830f31444

ian

August 27, 2009, August 27, 2009 18:30, permalink

No rating. Login to rate!

better late than never:

link_to(image_tag("go_back.png") + " Go Back", user_path(@user))
D41d8cd98f00b204e9800998ecf8427e

sdfsdfsdfsd

November 6, 2009, November 06, 2009 12:06, permalink

No rating. Login to rate!
<%= link_to image_tag("go_back.png"), user_path(@user) %><%= link_to 'Go Back', user_path(@user) %>fsdfsdfsd

sd
fs
f
sdfsd
1fda026b9259e2fd954f2c3eeb424e1c

megharaj

November 8, 2010, November 08, 2010 13:34, permalink

No rating. Login to rate!

sdfdsf
dsf
sd
f
sd

Your refactoring





Format Copy from initial code

or Cancel