RoR:嵌套属性未定义方法`build

RoR: nested attributes undefined method `build

我试图在 "client" 表格中放入一个嵌套的 fields_for 以同时添加他的地址。 关注了这个视频:https://www.youtube.com/watch?v=fsgTT9hizZo 显示了我需要的内容。

好吧,这是我的代码:

客户端模型 =>

class Cliente < ActiveRecord::Base

 has_one :local
 accepts_nested_attributes_for :local


end

本地模型=>

class Local < ActiveRecord::Base
 belongs_to :cliente


end

客户端控制器=>

def new
 @cliente = Cliente.new
 @cliente.build_local
end
def cliente_params
  params.require(:cliente).permit(:name, :telefone, :celular, :email, :local_attributes => [:logra, :cep, :uf, :city, :km])
end

客户观点

  <%= f.fields_for :local do |ff| %>

  <td>   <%= ff.text_field :km %> </td>

错误= # 的未定义方法“build_local” 在 cliente_controller.rb

在创建local对象之前,您首先需要创建cliente对象

def new
  @cliente = Cliente.new      
end

def show
  @cliente = Cliente.find(params[:id])
  @cliente.build_local
end

试试这个-

@cliente.build_local()

而不是-

@cliente.build_local