【发布时间】:2014-01-18 15:54:50
【问题描述】:
我正在使用与 has_many 关联的 3 个模型构建一个 rail 3.2.16 应用程序,但我的保存操作不起作用。这是我的模型:
class Cliente < ActiveRecord::Base
has_many :prestamos
accepts_nested_attributes_for :prestamos, reject_if: :all_blank
end
class User < ActiveRecord::Base
has_many :prestamos
has_many :clientes, :through => :prestamos
end
class Prestamo < ActiveRecord::Base
validates_numericality_of :monto, only_integer: true
validates :monto, presence: true
belongs_to :user
belongs_to :cliente, inverse_of: :prestamos
end
当我尝试在 clientes_controller#new 中构建我的@cliente 时
@cliente = current_user.clientes.build
这个在#create
@cliente = current_user.clientes.build(params[:cliente])
并使用此视图
<%= simple_form_for(@cliente) do |f| %>
<%= f.input :nombre %>
<%= f.input :cedula %>
<%= f.input :direccion %>
<%= f.simple_fields_for :prestamos do |builder| %>
<%= builder.input :monto %>
<% end %>
<%= f.button :submit %>
<% end %>
渲染 HTML 是预期的,但在保存时,我得到了与 prestamo 模型中的 monto 相关的验证错误。顺便说一下,monto字段显示了两次。
我的错误:
Prestamos monto 不是一个数字 Prestamos monto 不能为空
我真的希望有人可以帮助我。
提前致谢。
【问题讨论】:
-
您是否将
User模型中的prestamos_attributes列入白名单(使用attr_accessible)? -
不,我只是将它添加到我的客户模型中。顺便说一句,我只是添加它来测试它,但错误仍然存在。
-
能否将错误信息添加到问题中?
-
我刚刚添加了它。是验证错误、存在和数量
-
您使用了哪些值导致了这些错误?
标签: ruby-on-rails-3 has-many-through has-many