0

The nested form in the view just won't render, unless I remove the f attribute, in which case the submit button will not work. I have two models, job and employer. I've been following the railscast here

job.rb

  attr_accessible :title, :location, :employers_attributes,     
  belongs_to :employers
  accepts_nested_attributes_for :employers

employer.rb

 attr_accessible :companyname, :url
 has_many :jobs

jobs_controller.rb

  def new

    @job = Job.new
    @employer = Employer.new
  end

_form.html

<%= form_for(@job) do |f| %>


    <%= f.label :title %>
    <%= f.text_field :title %>


    <%= f.label :location %>
    <%= f.text_field :location %>



  <%= f.fields_for :employers do |builder| %>

        <%= builder.label :companyname, "Company Name" %>
        <%= builder.text_field :companyname %>


        <%= builder.label :url, "Web Address" %>
        <%= builder.text_field :url %>

    <% end %>


  <div class="actions">
    <%= f.submit %>
  </div>

 <% end %>

Any input would be brilliant - thanks

4

2 回答 2

2

发生这种情况是因为您的工作没有雇主。

将您的代码更改为:

def new
  @job = Job.new
  @job.employer = @job.build_employer
end

在你的 job.rb 改变:

attr_accessible :title, :location, :employer_attributes,     
belongs_to :employer
accepts_nested_attributes_for :employer
于 2013-05-20T17:17:01.373 回答
1

这一行:

belongs_to :employers

应该是单数:

belongs_to :employer 

使用此关联,您不需要嵌套表单,您可以使用 select 为每个工作挑选雇主。

但是,如果您的每项工作都需要多个雇主,并且每个工作都可以有多个雇主,请观看此截屏视频

于 2013-05-20T17:36:13.827 回答