无效的关联.确保 accepts_nested_attributes_for 用于 :mark_images 关联 [英] Invalid association. Make sure that accepts_nested_attributes_for is used for :mark_images association

查看:38
本文介绍了无效的关联.确保 accepts_nested_attributes_for 用于 :mark_images 关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是指

f.link_to_add

... 行.当我删除该行时,它工作正常.但后来我无法添加更多图片.

... line. When I remove that line, it works fine. But then I can't add more images.

<%= nested_form_for(:mark, url: place_mark_path, validate: true, html: {multipart: true}, 'data-update-target' => 'marks', class: 'marks') do |f| %>

  ...

  <%= f.text_field :title %>
  <%= f.check_box :mark %>

  <%= f.fields_for :mark_images do |p| %>
    <%= p.file_field :image %>
    <%= p.link_to_remove %><br>
  <% end %>

  <%= f.link_to_add "+ Add another image", :mark_images %>   <------ Problem

  <%= f.submit " Submit" %>

<% end %>

路线

  resources :places do
    resources :marks
  end

marks_controller

marks_controller

def new
    query = @factual.table('places')
    @place = query.filters('factual_id' => params[:place_id]).first
    @mark = Mark.new
    @mark.mark_images.build
end

标记模型

  has_many :mark_images, :as => :attachable, :dependent => :destroy
  accepts_nested_attributes_for :mark_images

mark_images 模型

mark_images model

  belongs_to :attachable, polymorphic: true
  mount_uploader :image, ImageUploader 

推荐答案

改变这一行

<%= nested_form_for(:mark, url: place_mark_path, validate: true, html: {multipart: true}, 'data-update-target' => 'marks', class: 'marks') do |f| %>

这应该有效

<%= nested_form_for(@mark, url: place_mark_path, validate: true, html: {multipart: true}, 'data-update-target' => 'marks', class: 'marks') do |f| %>

这篇关于无效的关联.确保 accepts_nested_attributes_for 用于 :mark_images 关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆