我对 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和:text (在这种特殊情况下我正在尝试使用:title ):
<% @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
非常感谢您的任何帮助。