1

我尝试将代码从 haml 转换为 erb,但我被卡住了,不知道为什么。

这是我要转换的原始代码:https ://github.com/gmarik/simple-backend-example/blob/master/app/views/backend/resource/_index.html.haml

这就是我现在所拥有的。有人可以看看它并给我一些提示。谢谢。

我最怀疑这条线: %tr[resource]{odd_or_even}

我认为它可能是这样的: <tr> <% @resource{odd_or_even} %>

RubyMine 在这一行给了我一个错误: <%= paginate collection %>

    <% content_for(:header) do %>
      <h1><%= resource_class.model_name.human(count: 2) %></h1>

    <ul class="tabs">
       <li class="active"><%= link_to "Index", "#" %></li>
       <li><%= link_to "New", new_resource_path %> </li>
    </ul>
    <table class='zebra-striped'>
      <thead>
        <tr>
          <% attributes.each do |attr| %>
            <th> <%= resource_class.human_attribute_name(attr) %></th>
          <th> &nbsp;</th>
        </tr>
      </thead>
      <tbody>
        <% collection.each do |resource| %>
          <tr> <% @resource{odd_or_even} %>
            <% attributes.each do |attr| %>

            <td> <%= resource.public_send(attr).to_s.truncate(20) %> </td>        
            <td class='row-actions'>
              <%= link_to 'show', resource_path(resource) %>
              |
              <%= link_to 'edit', edit_resource_path(resource) %>
              |
              <%= link_to 'destroy', resource_path(resource), method: :delete, confirm: "Are you sure?" %>
            </td>
        <% end %> 
      </tbody> 
    </table>  
    <%= paginate collection %>
4

1 回答 1

1

在这里查看文档:http: //haml.info/docs/yardoc/file.REFERENCE.html#object_reference_

我会说它是:

<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= resource.class.name.underscore %>">

这是要翻译的%tr[resource]

现在,{odd_or_even}将只转换帮助程序的结果哈希odd_or_even以将它们映射为tr.

If we take a look at the definition of the method here : https://github.com/gmarik/simple-backend-example/blob/master/app/helpers/backend/application_helper.rb

We see it's just a call to cycle in order to set an extra class. Therefore we end up with:

<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= "#{resource.class.name.underscore} #{cycle("odd", "even", name: "rows")}" %>">

Now, all of this won't fix the problem of paginate. Add the error message if you're still stuck on this.

于 2012-11-11T02:25:33.907 回答