【发布时间】:2014-10-03 15:22:24
【问题描述】:
目前正在使用以下 railscast http://railscasts.com/episodes/196-nested-model-form-revised 处理嵌套表单
我有一个机会模型,
class Opportunity < ActiveRecord::Base
has_many :headings
accepts_nested_attributes_for :headings
end
标题模型
class Heading < ActiveRecord::Base
belongs_to :opportunity
has_many :subheadings
accepts_nested_attributes_for :subheadings
end
和一个副标题模型
class Subheading < ActiveRecord::Base
belongs_to :heading
end
现在我正在研究机会模型的新操作视图。
<h1>Add an opportunity</h1>
<%= form_for(@opportunity, :html => {:role => 'form'}) do |f| %>
<%= f.fields_for :headings do |builder| %>
<%= render 'heading_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add heading", f, :headings %>
<%= f.submit %>
<% end %>
还有 _heading_fields.html.erb 部分:
<fieldset>
<%= f.label :title, "Heading" %>
<%= f.text_field :title %>
<%= f.hidden_field :_destroy %>
<%= link_to "Remove section", '#', class: "remove_fields" %>
<%= link_to_add_fields "Add section", f, :subheadings %>
</fieldset>
最后是应用程序助手中的 link_to_add_fields 方法:
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end
现在我在 link_to_add_fields 函数中收到以下渲染调用错误:
syntax error, unexpected tIDENTIFIER, expecting keyword_end
它似乎与在 link_to_add_fields 方法中呈现的部分名称中的下划线有关,因为当下划线不存在时它会消失。
非常感谢任何帮助解决这个问题!
【问题讨论】:
-
你能粘贴完整的错误吗?
syntax error, unexpected tIDENTIFIER, expecting keyword_end这种类型的错误通常发生在你没有正确关闭循环时
标签: ruby-on-rails syntax-error render partial