所以,我关注了this tutorial但将 @article 与 current_user 或(我的用户模型)交换,并将 :attachments 与 :photos 或我的照片模型交换。
我要说的可能有帮助的一件事是我不确定他为什么引用 attachable?他不应该引用文章吗?或者在我的情况下是用户?
我收到这个错误:
PG::UndefinedColumn: ERROR: column photos.attachable_type does not exist
LINE 1: ..."photos" WHERE "photos"."attachable_id" = $1 AND "photos"."...
^
: SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2
我的表单片段:
<div class="tab-pane" id="tab2">
<%= nested_form_for current_user, :html=>{:multipart => true } do |f| %>
<%= f.fields_for :photos do |p| %>
<p>
<%= p.label :file %><br />
<%= p.file_field :file %>
</p>
<%= p.link_to_remove "Remove this attachment" %>
<% end %>
<%= f.link_to_add "Add attachment", :photos %>
<p><%= f.submit %></p>
<% end %>
</div>
user.rb 片段
has_many :photos, as: :attachable
accepts_nested_attributes_for :photos
photos.rb 完整
class Photo < ActiveRecord::Base
attr_accessible :file
belongs_to :attachable, :polymorphic => true
mount_uploader :file, FileUploader
end
最佳答案
您的照片模型 belongs_to :attachable
作为多态关联,这意味着它需要两列 - :attachable_type
(字符串)和 :attachable_id
(整数)。因此,例如,如果用户 ID #1 有一张照片,则照片的 attachable_type 将为“用户”,而 attachable_id 将为 1。
如果您还没有创建迁移,将这些字段添加到您的照片模型中。您可以通过(在迁移中)执行此操作:
change_table :photos do |t|
t.references :attachable, polymorphic: true
end
关于ruby-on-rails - PG::UndefinedColumn:错误:列 photos.attachable_type 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20277217/