繁体   English   中英

嵌套表单未定义方法“ model_name”

[英]Nested form undefined method `model_name'

我有处理3个模型的嵌套表格。 工作,雇主,用户

工作控制器上的表格需要创建工作,雇主和用户。

Job和Employer表单正常工作,但是当我添加User嵌套表单时,出现错误“ NilClass:Class的未定义方法'model_name'”

我完全困惑为什么。

这是我的代码:

工作模式

  attr_accessible :category, :employer_id, :employer_attributes, :user_attributes

  belongs_to :employer
  accepts_nested_attributes_for :employer, :user
  has_many :applications
  has_many :users, :through => :applications 

雇主模式

 attr_accessible :companyname, :email, :logo, :password, :url
has_many :jobs
 belongs_to :user

用户模型

attr_accessible :admin, :cv, :name, :password, :website, :password_confirmation

  has_many :applications
  has_many :jobs, :through => :applications
  has_one :employer

_form.html.erb

<%= form_for(@job) do |f| %>
    <%= f.label :title %>
    <%= f.text_field :title %>

<%= f.fields_for :employer do |builder| %>
     <%= builder.label :companyname, "Company Name" %>
      <%= builder.text_field :companyname %>
<% end %>

<%= f.fields_for :user do |builder| %>
    <%= builder.label :email, "Email" %>
     <%= builder.text_field :email %>
 <%= builder.label :password, "Password" %>
        <%= builder.text_field :password %>
<% end %>

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

<% end %>

工作控制器

def new

  @job = Job.new
  @job.employer = @job.build_employer
  @job.user = @job.build_user

To看起来您的Job模型没有用户方法。 您可能需要添加

belongs_to :user

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM