编写新手的Rails代码的更好方法

时间:2013-05-05 04:10:33

标签: ruby-on-rails

我刚刚开始使用Rails(作为一个完整的编码新手),虽然我得到了这个代码,但我知道它会让经验丰富的编码员感到畏缩。请帮我改写一下这个。具体来说,以便第一张图像成为链接的一部分

<table class="table table-hover">
    <tr id="topbar"><th width="60%">Name</th> 
    <th width="10%">Size</th>
    <th width="20%">Modified</th>
    <th width="5%"></th>
    <th width="5%"></th></tr>
<% @folders.each do |folder| %>
    <tr>
     <td width="60%"><img src="https://s3-us-west-2.amazonaws.com/images/folder.gif"> <%= link_to folder.name, browse_path(folder) %></td>
     <td width="10%">-</td>
     <td width="20%">-</td>     
     <td width="5%"><%= link_to image_tag("https://s3-us-west-2.amazonaws.com/images/page_edit.gif"), rename_folder_path(folder) %></td> 
     <td width="5%"><%= link_to image_tag("https://s3-us-west-2.amazonaws.com/images/action_stop.gif"), folder, :confirm => 'Are you sure you want to delete the folder and all of its contents?', :method => :delete %></td>
    </tr>
<% end %> 

3 个答案:

答案 0 :(得分:2)

可以使用原始助手。

<td width="60%">
  <%= link_to raw("#{image_tag('https://s3-us-west-2.amazonaws.com/images/folder.gif')} #{folder.name}"), browse_path(folder) %>
</td>

答案 1 :(得分:0)

<%= link_to " <td width=\"60%\"><img src=\"https://s3-us-west-2.amazonaws.com/images/folder.gif\">#{link_to folder.name, browse_path(folder)}</td><td width=\"10%\">-</td><td width=\"20%\">-</td>" %>

未经测试,但我想象类似的东西应该有效。

答案 2 :(得分:0)

您可以使用ruby的块语法将图像和名称作为块传递,方法是使用do和end并将路径作为link_to的参数传递。

<td width="60%">
  <%= link_to browse_path(folder) do %>
    <img src="https://s3-us-west-2.amazonaws.com/images/folder.gif">
    <%= folder.name %>
  <% end %>
</td>