3

我想自定义 Rails 脚手架中的“新”和“编辑”页面。我在哪里可以自定义它。

任何人都可以为我提供一个教程。

我试图覆盖默认的 _form.html.erb 页面并出现错误

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>

我的 _form.html.erb 页面是

<%% 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 %> 

我将我的页面更新为:

<% 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 %> 

现在错误是:

(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    "
      ^
4

2 回答 2

2

通过将表单页面编辑为解决它

<%% 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 %>  
于 2012-11-05T07:22:57.397 回答
0

也许

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

应该是

<% form_for(@singular_name) do |f| %>

PS:
请使用 case-when-then-else attribute.type 代替 if-elseif-else-end
PPS:
使用 haml 代替 erb,你的代码看起来更易读

- 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"
      .field
        .label
          = f.label "#{attribute.name}".to_sym
          - 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
            - else
              = f.text_field attribute.name.to_sym
        - 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
于 2012-11-01T03:46:02.263 回答