我正在尝试使用回形针 gem 将多个文件上传到 S3 并参考本教程。我有两个问题 1. 上传标签只呈现一次,而不是控制器中指定的 6 次。2.其次,每当我尝试编辑文档时,都会出现以下错误。
DocumentsController#edit 中的 ActiveRecord::UnknownAttributeError
未知属性:document_id
任何建议如何解决这个问题?
模型/文档.rb
class Document < ActiveRecord::Base
attr_accessible :documentId, :name, :notes, :user_id
belongs_to :user
has_many :photos, :dependent=>:destroy
accepts_nested_attributes_for :photos
acts_as_taggable
validates :name, :length => {:minimum =>3}
validates_date :dtReminder, :on_or_after => lambda { Date.current }, :allow_blank => true
validates_associated :user
validates_uniqueness_of :name, :scope => :user_id
end
模型/照片.rb
class Photo < ActiveRecord::Base
belongs_to :document
has_attached_file :image, :style =>{:thumb => '150x150#',
:medium => '300x300>',
:large => '600x600>'
}
validates_attachment_size :photo, :less_than => 500.kilobytes
end
控制器/document_Controller.rb
def new
@document = Document.new
@document.user = current_user
5.times {@document.photos.build}
respond_to do |format|
format.html # new.html.erb
format.json { render json: @document }
end
end
def create
@document = Document.new(params[:document])
@document.user = current_user
5.times {@document.photos.build}
respond_to do |format|
if @document.save
format.html { redirect_to user_documents_path(current_user), notice: 'Document was successfully created.' }
format.json { render json: @document, status: :created, location: @document }
else
format.html { render action: "new" }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end
end
视图/文档/_form.html.erb
<%= form_for(document) do |f| %>
<table>
<tr>
<td><%= f.label :name %></td>
<td><%= f.text_field :name %></td>
</tr>
<tr>
<td><%= f.label :notes %></td>
<td><%= f.text_area :notes %></td>
</tr>
<tr>
<td>Photos</td>
<td>
<%= fields_for :photos do |photo|%>
<%= photo.file_field :image%>
<%end%>
</td>
</tr>
<tr>
<td></td>
<td><%= f.submit %></td>
</tr>
</table>
<% end %>