Rails 3:link_to和image_tag

时间:2021-09-10 00:27:16

How can I have an image tag linking to the file background.jpg in my public/images and when clicked on redirect the user to the root_url (So the content of the page is in a container and if the user clicks on the background image are redirected to home?) Thanks!

如何在我的公共/图像中链接到文件background.jpg的图像标记,并在单击时将用户重定向到root_url(因此页面的内容位于容器中,如果用户点击背景图像,则重定向到家?)谢谢!

3 个解决方案

#1


14  

Sure:

当然:

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

#2


6  

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

大多数rails标签助手接受一个块,这意味着你可以使用do来简化你的生活。这里我使用了一个带有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 emits what the anchor wraps.

在这种情况下,link_to期望下一个参数是路径,并且块发出锚包装的内容。

In general I try to keep to the one tag helper per line rule unless its super trivial, so prefer this technique when the line would otherwise become crowded. An advantage to doing it this way is since tags are on different lines, errors are easier to identify.

一般来说,我试图保持每行标准的一个标签助手,除非它超级琐碎,所以当线条否则变得拥挤时更喜欢这种技术。这样做的一个好处是,由于标签位于不同的行上,因此更容易识别错误。

If you need other options to go with it, add them like you usually would. For example I'll add a css class:

如果您需要其他选项,请像平常一样添加它们。例如,我将添加一个css类:

<%= link_to root_url, :class => 'imagelink' do %>
  ...

#3


4  

I was searching for same thing while ago, and i found the Rails way to do so.

我刚才在寻找同样的东西,我找到了Rails的方法。

Lets say you want to you HTML page to render follwoing code

让我们说你想要HTML页面来渲染下面的代码

<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 way.

“页面”是控制器,“主页”是操作。这是Rails方式。

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

#1


14  

Sure:

当然:

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

#2


6  

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

大多数rails标签助手接受一个块,这意味着你可以使用do来简化你的生活。这里我使用了一个带有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 emits what the anchor wraps.

在这种情况下,link_to期望下一个参数是路径,并且块发出锚包装的内容。

In general I try to keep to the one tag helper per line rule unless its super trivial, so prefer this technique when the line would otherwise become crowded. An advantage to doing it this way is since tags are on different lines, errors are easier to identify.

一般来说,我试图保持每行标准的一个标签助手,除非它超级琐碎,所以当线条否则变得拥挤时更喜欢这种技术。这样做的一个好处是,由于标签位于不同的行上,因此更容易识别错误。

If you need other options to go with it, add them like you usually would. For example I'll add a css class:

如果您需要其他选项,请像平常一样添加它们。例如,我将添加一个css类:

<%= link_to root_url, :class => 'imagelink' do %>
  ...

#3


4  

I was searching for same thing while ago, and i found the Rails way to do so.

我刚才在寻找同样的东西,我找到了Rails的方法。

Lets say you want to you HTML page to render follwoing code

让我们说你想要HTML页面来渲染下面的代码

<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 way.

“页面”是控制器,“主页”是操作。这是Rails方式。

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