私は2つのモデルを持っています:
ビデオ:
class Video < ActiveRecord::Base
belongs_to :user
has_many :thumbnails
attr_accessor :search, :saveable
accepts_nested_attributes_for :thumbnails, :allow_destroy => true
en
d
サムネイル:
class Thumbnail < ActiveRecord::Base
belongs_to :video
end
動画を検索するために YouTubeG gem を使用しています。
検索によって返される各ビデオには、ビューにフォームがあります。
<% form_for :video, :url => videos_path, :html => { :class => :form } do |f| -%>
<%= f.hidden_field :url, :value => video.unique_id %>
<%= f.hidden_field :name, :value => video.title %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<% if video.thumbnails.present? %>
<% f.fields_for :thumbnails, video do |t| %>
<% video.thumbnails.each do |thumbnail| -%>
<%=image_tag thumbnail.url %>
<%=t.text_field :url, :value => thumbnail.url %>
<% end -%>
<% end -%>
<% end %>
<%= f.submit "Save" %>
<% end -%>
f.fields_for :thumbnails は生成します
<input type="hidden" value="http://i.ytimg.com/vi/s8eigkwmMEo/0.jpg" name="video[thumbnails][url]" id="video_thumbnails_url"/>
このビデオのすべてのサムネイルを保存したいので、これは間違っているようです。
保存しようとすると、
ActiveRecord::VideosController の AssociationTypeMismatch#create
パラメーター:
{"commit"=>"保存", "video"=>{"name"=>"カラヤン - ベートーベン交響曲第 7 番", "url"=>"s8eigkwmMEo", "user_id"=>"1","サムネイル"=>{"url"=>" http://i.ytimg.com/vi/s8eigkwmMEo/0.jpg "}}} < 4 つのサムネイルがあるはずです