我有此操作来更新数据:
def edit
@project = Project.find(params[:id])
if @project.team
@team = Team.find(@project.id)
else
@team = Team.new
end
end
形式:
= form_for @project do |f|
...
= f.fields_for @team do |t|
#if I use following: = f.fields_for :team do |t|, then the form inputs in this form are not displayed
...
end
end
模型:
class Project < ActiveRecord::Base
has_one :team
accepts_nested_attributes_for :team
end
class Team < ActiveRecord::Base
belongs_to :project
end
当我尝试发送表单时,我收到以下错误消息
Team(#2501295380) expected, got ActiveSupport::HashWithIndifferentAccess(#2157764620)
我在SO上找到了类似的帖子,但是没有人帮助我解决这个问题,这就是为什么我会非常感谢每一个建议。
非常感谢
这解决了我的问题:
def edit
@project = Project.find(params[:id])
unless @project.team.nil?
@project.team
else
@project.build_team
end
end