Rails generators are very helpful tools and I wanted to customize existing ones for views. I’m new to Ruby, so I wanted to ease my workflow. I generated “user” model with default scaffold templates. Then I modified it a little to put some Twitter Bootsrap and generated “brand” model. But at some point I broke it up (I think). Now I have some problems. I use Rails 4 and modifying template files that stay in the folder of the railties gem: “D:\Bitnami\rubystack-2.0.0-23\ruby\lib\ruby\gems\2.0.0\gems\railties-4.1.7\lib\rails\generators\erb\scaffold\templates”.
Here is code from “index.html.erb” template file:
<%% 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" %>
Everything is fine except this part:
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<th><%= attribute.human_name %></th>
<% end -%>
And this one:
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end -%>
They just don’t generate anything. I don’t exactly know why scaffolding generator behaves so. First I thought that I have mistyped something. Before modifying I had copied template files, in order to use them when I’m mistaken. So I copy-pasted parts of code containing
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<% end -%>
loop. Not worked.
Then I supposed there are mistakes with “color” model. To check that I generated only “view” files for last successful generated model called “brand”:
rails g scaffold brand --migration=false --skip
And this time generator worked with the same mistakes (just nothing instead of
<th><%= t("brand.brandName") %></th>
and
<td><%= brand.brandName %></td>
). How could I fix that or there is a bug with my Rails installation?