ruby-on-railserbscaffoldingcustomizing

Customize rails scaffolding template


I want to customize the 'new' and 'edit' pages in rails scaffolding. Where can i coustomize it.

can any1 please provide me a tutorial for that.

I tried to override the default _form.html.erb page and got error

create  app/models/
      exists  app/controllers/
      exists  app/helpers/
      exists  app/views/books
      create  app/views/layouts/
      create  test/functional/
      create  test/unit/
      create  test/unit/helpers/
      create  public/stylesheets/
      create  app/views/books/index.html.erb
      create  app/views/books/show.html.erb
      create  app/views/books/new.html.erb
      create  app/views/books/edit.html.erb
      create  app/views/books/_form.html.erb
undefined local variable or method `f' for #<Rails::Generator::Commands::Create:0xb70eae04>

My _form.html.erb page is

<%% form_for(@<%= singular_name %>) do |f| %>
  <%%= f.error_messages %>

<% for attribute in attributes -%>
      <% if attribute.name != "id" && attribute.name !="created_at" && attribute.name !="updated_at" %>  
      <div class="field">  
        <div class="label">  
          <%= f.label "#{attribute.name}".to_sym  %>  
      </div>  

        <% if attribute.type == :integer || attribute.type == :float || attribute.type == :string %>  
        <% if attribute.name =~ /_id$/ # is this a id/foreign key field %>  
          <% attribute_class = attribute.name.gsub(/_id$/, '').classify.constantize %>    
          <% if attribute_class %>  
            <%= collection_select(attribute.class.name.underscore.to_sym, attribute.name.to_sym, attribute_class.all, :id, :name, :prompt => true) %>  
          <% else %>  
            <%= f.text_field attribute.name.to_sym  %>  
          <% end %>  
        <% else %>  
          <%= f.text_field attribute.name.to_sym  %>  
        <% end %>  
      <% elsif attribute.type == :text %>  
        <%= f.text_area attribute.name.to_sym  %>  
      <% elsif attribute.type == :datetime %>  
        <%= f.datetime_select attribute.name.to_sym  %>    
      <% elsif attribute.type == :boolean %>  
        <%= f.check_box attribute.name.to_sym  %>        
      <% else %>  
        <% # Unknown attribute Type %>  
      <% end %>       
      </div>  
    <% end %>   
    <% end %> 

I updated my page as:

<% form_for(@<%= singular_name %>) do |f| %>
      <%= f.error_messages %>

    <% for attribute in attributes -%>
          <% if attribute.name != "id" && attribute.name !="created_at" && attribute.name !="updated_at" %>  
          <div class="field">  
            <div class="label">  
              <%= f.label "#{attribute.name}".to_sym  %>  
          </div>  

            <% if attribute.type == :integer || attribute.type == :float || attribute.type == :string %>  
            <% if attribute.name =~ /_id$/ # is this a id/foreign key field %>  
              <% attribute_class = attribute.name.gsub(/_id$/, '').classify.constantize %>    
              <% if attribute_class %>  
                <%= collection_select(attribute.class.name.underscore.to_sym, attribute.name.to_sym, attribute_class.all, :id, :name, :prompt => true) %>  
              <% else %>  
                <%= f.text_field attribute.name.to_sym  %>  
              <% end %>  
            <% else %>  
              <%= f.text_field attribute.name.to_sym  %>  
            <% end %>  
          <% elsif attribute.type == :text %>  
            <%= f.text_area attribute.name.to_sym  %>  
          <% elsif attribute.type == :datetime %>  
            <%= f.datetime_select attribute.name.to_sym  %>    
          <% elsif attribute.type == :boolean %>  
            <%= f.check_box attribute.name.to_sym  %>        
          <% else %>  
            <% # Unknown attribute Type %>  
          <% end %>       
          </div>  
        <% end %>   
        <% end %> 

Now error is :

(erb):32:in `template': compile error (SyntaxError)
(erb):1: syntax error, unexpected $undefined, expecting ')'
_erbout = '';  form_for(@<%=singular_name; _erbout.conc...
                         ^
(erb):32: syntax error, unexpected kEND, expecting $end
;  end ; _erbout.concat "   \n    "
      ^

Solution

  • Solved it by editing form page as

    <%% for column in object.class.columns %>  
        <%% if column.name != "id" && column.name !="created_at" && column.name !="updated_at" %>
        <div class="field">  
            <div class="label">  
              <%%= f.label "#{column.name}".to_sym  %>  
          </div>  
    
            <%% if column.type == :integer || column.type == :float || column.type == :string %>  
            <%% if column.name =~ /_id$/ # is this a id/foreign key field %>  
              <%% column_class = column.name.gsub(/_id$/, '').classify.constantize %>    
              <%% if column_class %>  
                <%%= collection_select(object.class.name.underscore.to_sym, column.name.to_sym, column_class.all, :id, :name, :prompt => true) %>  
              <%% else %>  
                <%%= f.text_field column.name.to_sym  %>  
              <%% end %>  
            <%% else %>  
              <%%= f.text_field column.name.to_sym  %>  
            <%% end %>  
          <%% elsif column.type == :text %>  
            <%%= f.text_area column.name.to_sym  %>  
          <%% elsif column.type == :datetime %>  
            <%%= f.datetime_select column.name.to_sym  %>    
          <%% elsif column.type == :boolean %>  
            <%%= f.check_box column.name.to_sym  %>        
          <%% elsif column.type == :date %>  
             <%%= f.text_field column.name.to_sym, :id=>"date_picker"  %>       
          <%% else %>  
            <%% # Unknown Column Type %>  
          <%% end %>       
          </div>  
        <%% end %>   
        <%% end %>