2

In the ERB for scaffold generators in Rails 4.2 from where do the variables / methods come from in the template?

Things like singular_table_name and attributes in the below '_form.html.erb' template. Are they methods? variables? where can I find a list of what is available for use in this template?

I tried using some ActiveRecord class methods for example (reflections) and they were not available.

If it is only a set list of methods, not drawn from the AR class, is there a way to pass instance variables from the generators like you would from a controller to a view?

Originally from .rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/generators/erb/scaffold/templates/_form.html.erb

I now placed this file (which works) in rails_app/lib/templates/erb/scaffold

<%%= form_for(@<%= singular_table_name %>) do |f| %>
  <%% if @<%= singular_table_name %>.errors.any? %>
    <div id="error_explanation">
      <h2><%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>

      <ul>
      <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %>
        <li><%%= message %></li>
      <%% end %>
      </ul>
    </div>
  <%% end %>

<% attributes.each do |attribute| -%>
  <div class="field">
<% if attribute.password_digest? -%>
    <%%= f.label :password %><br>
    <%%= f.password_field :password %>
  </div>
  <div class="field">
    <%%= f.label :password_confirmation %><br>
    <%%= f.password_field :password_confirmation %>
<% else -%>
    <%%= f.label :<%= attribute.column_name %> %><br>
    <%%= f.<%= attribute.field_type %> :<%= attribute.column_name %> %>
<% end -%>
  </div>
<% end -%>
  <div class="actions">
    <%%= f.submit %>
  </div>
<%% end %>
Axiombadger
  • 161
  • 10
  • For the moment I realise I can just have the template create the code for an erb view where I can pass things from the controller at runtime. I am still interested in where the template variable scope comes from though. – Axiombadger Apr 24 '15 at 10:28
  • 2
    The command instance_variables returns all the methods I was looking for in the scope. I am still interested in passing extra variables in. [:@inside_template, :@behavior, :@_invocations, :@_initializer, :@options, :@attributes, :@name, :@args, :@shell, :@destination_stack, :@in_group, :@after_bundle_callbacks, :@class_path, :@file_name, :@controller_name, :@controller_class_path, :@controller_file_name, :@parent_options, :@controller_file_path, :@source_paths, :@output_buffer, :@plural_name, :@table_name, :@plural_table_name, :@singular_table_name, :@human_name] – Axiombadger Apr 24 '15 at 22:17

0 Answers0