我是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 ,例如:标题,:short_text 和:文字作为静态文字(在这种特殊情况下,我正在尝试使用:title ):< / p>
<% @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>
我的 job.rb 文件如下所示:
class Job < ActiveRecord::Base
has_many :job_descriptions, :dependent => :destroy
accepts_nested_attributes_for :job_descriptions
def self.languages
[:de, :en]
end
end
非常感谢您提前寻求帮助。
答案 0 :(得分:0)
<%= form_for @job do |form| %>
...
<%= form.fields_for :job_descriptions do |subform| %>
<% if subform.object.active? %>
<%= subform.text_field :title %>
<% end %>
<% end %>
...
<% end %>