具有多态对象的嵌套表单的create方法中的未知属性错误

时间:2011-12-08 12:24:25

标签: ruby-on-rails paperclip polymorphic-associations

我正在尝试使用一个简单的嵌套表单来处理has_many belongs_to多态关联。表单正在渲染字段,但是当它提交时,它会从create方法中产生一个“未知属性”错误。以下是相关代码:

user.rb

class User < ActiveRecord::Base
  has_many :images, :as => :imageable
  accepts_nested_attributes_for :images  
end

users_controller.rb

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
    @user.images.build
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      redirect_to @user
    else
      render 'new'
    end
  end
end

视图/用户/ new.html.erb

<%= form_for @user, :html => { :multipart => true } do |form| %>

    <%= form.fields_for :images, @user.images do |builder| %>
        <%= builder.file_field :images %>
    <% end %>

    <%= form.submit "Create user" %>
<% end %>

image.rb

class Image < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true

  has_attached_file :image_resource
end

我正在使用paperclip gem作为image.rb中的has_attached_file方法。确切的错误是“未知属性:图像”

1 个答案:

答案 0 :(得分:0)

哎呀这里唯一的问题是我应该是&lt;%= builder.file_field:image_resource%&gt;