在Ruby on Rails中自定义脚手架视图

时间:2015-03-05 11:32:30

标签: ruby-on-rails ruby-on-rails-4 scaffolding scaffold

Rails生成器是非常有用的工具,我想为视图自定义现有的工具。我是Ruby的新手,所以我想简化我的工作流程。我用默认的脚手架模板生成了“用户”模型。然后我稍微修改了一些Twitter Bootsrap并生成了“品牌”模型。但在某些时候我把它分解了(我想)。现在我有一些问题。我使用Rails 4并修改保留在railties gem文件夹中的模板文件:“D:\ Bitnami \ ruby​​stack-2.0.0-23 \ ruby​​ \ lib \ ruby​​ \ gems \ 2.0.0 \ gems \ railties-4.1。 7个\ LIB \轨道\发电机\ ERB \支架\模板”。

以下是“index.html.erb”模板文件中的代码:

<%% if (notice) %>
    <div class="alert alert-success fade in">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
      <p id="notice"><%%= notice %></p>
    </div>
<%% end %>

<table class="table table-striped">
  <thead>
    <tr>
  <% attributes.reject(&:password_digest?).each do |attribute| -%>
      <th><%= attribute.human_name %></th>
  <% end -%>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
      <tr>
<% attributes.reject(&:password_digest?).each do |attribute| -%>
    <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end -%>
        <td><div style="float: right"><%%= link_to t("Show"), <%= singular_table_name %>, :class => "btn btn-success" %>
        <%%= link_to t("Edit"), edit_<%= singular_table_name %>_path(<%= singular_table_name %>), :class => "btn btn-warning" %>
        <%%= link_to t("Destroy"), <%= singular_table_name %>, method: :delete, data: { confirm: t("Warn.Areyousure") }, :class => "btn btn-danger" %></div></td>
      </tr>
    <%% end %>
  </tbody>
</table>

<br>

<%%= link_to t("<%= singular_table_name %>.New<%= human_name %>"), new_<%= singular_table_name %>_path, :class => "btn btn-default" %>

除了这一部分,一切都很好:

<% attributes.reject(&:password_digest?).each do |attribute| -%>
      <th><%= attribute.human_name %></th>
  <% end -%>

这一个:

<% attributes.reject(&:password_digest?).each do |attribute| -%>
    <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end -%>

他们只是不生成任何东西。 我不知道为什么脚手架发电机会这样做。首先,我认为我输错了一些东西。在修改之前我已经复制了模板文件,以便在我弄错时使用它们。所以我复制粘贴了包含

的代码部分
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<% end -%>

环。没用。

然后我认为“颜色”模型有错误。要检查我是否仅为上次成功生成的名为“品牌”的模型生成“查看”文件:

rails g scaffold brand --migration=false --skip

而这一次发电机工作时遇到了同样的错误(只有一点而不是

<th><%= t("brand.brandName") %></th>

<td><%= brand.brandName %></td>

)。 我怎么能解决这个问题,或者我的Rails安装有错误?

0 个答案:

没有答案