将嵌套表单text_field显示为索引页中的文本



我对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

<% @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

事先非常感谢您的帮助。

<%= form_for @job do |form| %>
  ...
  <%= form.fields_for :job_descriptions do |subform| %>
    <% if subform.object.active? %>
      <%= subform.text_field :title %>
    <% end %>
  <% end %>
  ...
<% end %>

相关内容

  • 没有找到相关文章

最新更新