0

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?

candle
  • 1,963
  • 3
  • 15
  • 24
  • It used while generation. Something like escape character. For example, the following escaped ERB tag would be needed in the template (note the extra %)...<%%= stylesheet_include_tag :application %> ...to generate the following output: <%= stylesheet_include_tag :application %> "http://guides.rubyonrails.org/generators.html" Rail Guides : Creating and Customizing Rails Generators & Templates – candle Mar 05 '15 at 11:44
  • So what is attributes? is this any variable comming from controller OR you are trying to list all attributes? – SSR Mar 05 '15 at 11:51
  • attributes means field value? or any value coming from DB? – SSR Mar 05 '15 at 11:54
  • They are listed in schema.rb. I have just generated model and controller with scaffold ("color colorName:string colorRGBCode:string") and editing just views. – candle Mar 05 '15 at 11:56
  • "Attributes" variable coming from controller? – SSR Mar 05 '15 at 11:57
  • I've used scaffolding by default , and actually don't know if they are coming from controller. Sorry, I'm a newbie in Rails. – candle Mar 05 '15 at 11:59
  • Okay nop! Actually what you want to do? – SSR Mar 05 '15 at 11:59
  • Want to list all field name, like Id, Name, Address? – SSR Mar 05 '15 at 12:00
  • Yes. As the header of .
    – candle Mar 05 '15 at 12:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72323/discussion-between-candle-and-ssr). – candle Mar 05 '15 at 12:03

0 Answers0