关联无效。确保accepts_nested_attributes_for用于:mark_images关联

时间:2014-05-04 22:13:54

标签: ruby-on-rails ruby-on-rails-4

错误指的是

  

f.link_to_add

...行。当我删除该行时,它工作正常。但是我无法添加更多图像。

<%= 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

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模型

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

1 个答案:

答案 0 :(得分:3)

更改此行

<%= 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| %>