在Ruby on Rails中编辑自定义视图模板时出错

时间:2012-10-29 06:11:53

标签: ruby-on-rails customization erb scaffolding

我正在尝试在ROR中创建自定义视图页面,如http://blog.hulihanapplications.com/browse/view/55-ruby-on-rails-multiple-model-form-generator

中所示

然后我遇到了一些错误

(erb):6:in `template': compile error (SyntaxError)
(erb):3: syntax error, unexpected $undefined
...orm", :locals => {:object => @<%= singular_name ).to_s); _er...
                              ^
(erb):3: syntax error, unexpected '}', expecting ')'
...s); _erbout.concat ", :f => f} %> \n\n<%= link_to 'Show', @"
                              ^
(erb):5: syntax error, unexpected tIDENTIFIER, expecting ')'
...out.concat " %> |\n<%= link_to 'Back', "
                              ^
(erb):6: syntax error, unexpected tIDENTIFIER, expecting ')'
; _erbout.concat(( plural_name ).to_s); _erbout.concat "_path %>\n"
                                                             ^
(erb):6: unterminated string meets end of file
(erb):6: syntax error, unexpected $end, expecting ')'
; _erbout
         ^

我的编辑页面是

<h1>Editing <%= singular_name %></h1>

<%= render :partial => "_form", :locals => {:object => @<%= singular_name %>, :f => f} %> 

<%%= link_to 'Show', @<%= singular_name %> %> |
<%%= link_to 'Back', <%= plural_name %>_path %>

我的部分表格“表格”是

    <% for column in object.class.columns %>  
      <% if column.name != "id" %>  
      <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  %>        
      <% else %>  
        <% # Unknown Column Type %>  
      <% end %>       
      </div>  
    <% end %>   
    <% end %>  

这是默认的编辑页面

<h1>Editing <%= singular_name %></h1>

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

<% for attribute in attributes -%>
  <p>
    <%%= f.label :<%= attribute.name %> %><br />
    <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
  </p>
<% end -%>
  <p>
    <%%= f.submit 'Update' %>
  </p>
<%% end %>

<%%= link_to 'Show', @<%= singular_name %> %> |
<%%= link_to 'Back', <%= plural_name %>_path %>

2 个答案:

答案 0 :(得分:2)

通过更改_form.html.erb

解决了我的问题
<%% 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 %>  

答案 1 :(得分:0)

您正在尝试将ERB放入ERB中,但您应该将Ruby放入ERB中。我想这个:

<%= render :partial => "_form", :locals => {:object => @<%= singular_name %>, :f => f} %> 

应该是这样的:

<%= render :partial => "_form", :locals => {:object => singular_name, :f => f} %> 

<%= ... %>内的所有内容都应该是Ruby代码,但@<%= singular_name %>不是Ruby。