如何使用Rails 4和CarrierWave从文件选择窗口上传多个图像?我有一个post_controller
和post_attachments
模型。我该怎么做呢?
有没有人能举个例子?有没有简单的方法来解决这个问题呢?
发布于 2015-07-03 14:17:04
此外,我还想出了如何更新多个文件上传,我也重构了它一点。这段代码是我的,但你明白我的意思。
def create
@motherboard = Motherboard.new(motherboard_params)
if @motherboard.save
save_attachments if params[:motherboard_attachments]
redirect_to @motherboard, notice: 'Motherboard was successfully created.'
else
render :new
end
end
def update
update_attachments if params[:motherboard_attachments]
if @motherboard.update(motherboard_params)
redirect_to @motherboard, notice: 'Motherboard was successfully updated.'
else
render :edit
end
end
private
def save_attachments
params[:motherboard_attachments]['photo'].each do |photo|
@motherboard_attachment = @motherboard.motherboard_attachments.create!(:photo => photo)
end
end
def update_attachments
@motherboard.motherboard_attachments.each(&:destroy) if @motherboard.motherboard_attachments.present?
params[:motherboard_attachments]['photo'].each do |photo|
@motherboard_attachment = @motherboard.motherboard_attachments.create!(:photo => photo)
end
end
发布于 2015-07-02 04:28:32
使用关联@post.post_attachments
时,不需要设置post_id
。
https://stackoverflow.com/questions/21411988
复制相似问题