尝试设置一个基本的 Rails 4 应用程序以用于学习目的时遇到错误,所以请多多包涵!我正在尝试创建一个应用程序来创建和显示自定义 Web 表单。我有一个表单模型,它有很多字段。我正试图让视图正常工作,这将允许我创建附加到特定表单的新字段记录:
class Form < ActiveRecord::Base
has_many :fields
end
class Field < ActiveRecord::Base
belongs_to :form
end
在我的字段索引视图中,我相信我已正确设置为仅显示特定表单的字段(通过 /forms/1/fields 之类的 url),我有一个链接:
<%= link_to 'New Field', new_form_field_path(@form) %>
fields/new.html.erb 文件有这个:
<h1>New field</h1>
<%= render :partial => 'form', :form => @form, :field => @field %>
fields/_form.html.erb 的开头是这样的:
<%= form_for(@form, @field) do |f| %>
fields_controller.rb 定义了这个方法:
def new
@form = Form.find(params[:form_id]) #unsure if this is necessary/correct, but its presence doesn't effect the error i'm getting
@field = Field.new
end
已经创建了一个 id 为 1 的表单。看起来 /forms/1/fields 正常。但是,当我单击“新字段”链接时,我会转到 /forms/1/fields/new,我收到以下错误:
Showing /home/moskie/Projects/FormBuilder/app/views/fields/_form.html.erb where line #1 raised:
can't write unknown attribute `html'
Extracted source (around line #1):
<%= form_for(@form, @field) do |f| %>
<% if @field.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@field.errors.count, "error") %> prohibited this field from being saved:</h2>
Trace of template inclusion: app/views/fields/new.html.erb
Rails.root: /home/moskie/Projects/FormBuilder
Application Trace | Framework Trace | Full Trace
app/views/fields/_form.html.erb:1:in `_app_views_fields__form_html_erb___1866877160086017450_70350628427620'
app/views/fields/new.html.erb:3:in `_app_views_fields_new_html_erb___1515443138224133845_70350627074400'
Request
Parameters:
{"form_id"=>"1"}
我对这个错误告诉我的内容感到非常困惑,所以我很难弄清楚我在这里做错了什么。谁能帮我吗?谢谢。