我对Rails完全陌生,所以我对这个愚蠢的问题感到抱歉:
这是生成我的表单及其嵌套表单的代码:
<%= form_for @job do |form| %>
<div><%= form.label :department %><%= form.text_field :department %></div>
<div><%= form.label :enabled %><%= form.check_box :enabled %></div>
<!-- job description form -->
<%= form.fields_for :job_descriptions do |subform| %>
<div><%= subform.hidden_field :language %></div>
<div><%= subform.object.language %></div>
<div><%= subform.label "Job Name" %><%= subform.text_field :title %></div>
<div><%= subform.label "Header" %><%= subform.text_field :short_text %></div>
<div><%= subform.label "Content" %><%= subform.text_area :text %></div>
<% end %>
<!-- end of job description form -->
<div><%= form.submit %></div>
<% end %>
虽然这是index.html.erb的一部分,但我想在其中显示子窗体中的text_fields,如:title、:short_text和:texttitle 我得到的错误是: 我的job.rb文件如下所示: 事先非常感谢您的帮助。<% @jobs.each do |job| %>
<li>
<%= job.title %>
<%= job.department %>
<%= job.enabled %>
<%= link_to 'Show', job_path(job) %>
<%= link_to 'Delete', job_path(job), method: :delete, data: { confirm: 'Are you sure?' } %>
</li>
<% end %>
undefined method `title' for #<Job:0x007fcdde2001a8>
class Job < ActiveRecord::Base
has_many :job_descriptions, :dependent => :destroy
accepts_nested_attributes_for :job_descriptions
def self.languages
[:de, :en]
end
end
<%= form_for @job do |form| %>
...
<%= form.fields_for :job_descriptions do |subform| %>
<% if subform.object.active? %>
<%= subform.text_field :title %>
<% end %>
<% end %>
...
<% end %>