我想自定义 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 "
^