gpt4 book ai didi

ruby-on-rails - 强参数不允许使用 json 创建嵌套属性。 ActiveRecord::AssociationTypeMismatch 错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:18:25 25 4
gpt4 key购买 nike

我有两种模式: session 和会谈。 session 有_many talks and

accepts_nested_attributes_for :talks, :allow_destroy => true

我发布的参数看起来像这样:

{"id"=>"2", "name"=>"rails4444", "tags"=>"ruby, rails, backbone, javascript", "date"=>"2014-02-22", "organizer"=>"BackboneMeetupGroup", "description"=>"conference with cool speakers", "talks"=>{"title"=>"fdsf", "video_url"=>"fdsf"}, 

我在哪里设置会谈,因为应该在数据库中创建。

我的 html 看起来像这样:

<input type="text" name="talks[title]" placeholder="Talk Title" />
<input type="text" name="talks[video_url]" placeholder="url" />

当我用演讲更新我的 session 模型时,它给我一个错误:

ActiveRecord::AssociationTypeMismatch(预期对话(#70154003251480),得到数组(#70154000241560)): app/controllers/conferences_controller.rb:25:在“更新”中

我的 Controller 看起来像这样:

 def update
@single = Conference.find params[:id]
if @single.update_attributes conference_params
render "conferences/show"
else
respond_with @single
end
end



def conference_params
params.permit(:name, :tags, :date, :organizer, :description, :place, :talks => [:conference_id, :id, :title, :video_url])
end

为什么会出现 ActiveRecord::AssociationTypeMismatch 错误,我该如何解决?

最佳答案

作为conference has_many talks,您需要一组作为嵌套属性的演讲,而不是一个。

您的输入字段需要这样的名称:

<input type="text" name="conference[talks_attributes][0][title]" />

使用accepts_nested_attributes。创建这些输入的正常方法是使用

<%= fields_for :talks do |ff| %>
<%= ff.text_field :title %>
<% end %>

参见 Rails Guide on form helpers .

并且您需要按照 medBo 的建议在 permit 中使用 talks_attributes

关于ruby-on-rails - 强参数不允许使用 json 创建嵌套属性。 ActiveRecord::AssociationTypeMismatch 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22518772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com