首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Rails 4使用carrierwave上传多个图像或文件

Rails 4使用carrierwave上传多个图像或文件
EN

Stack Overflow用户
提问于 2014-01-29 01:00:31
回答 2查看 73.4K关注 0票数 86

如何使用Rails 4和CarrierWave从文件选择窗口上传多个图像?我有一个post_controllerpost_attachments模型。我该怎么做呢?

有没有人能举个例子?有没有简单的方法来解决这个问题呢?

EN

回答 2

Stack Overflow用户

发布于 2015-07-03 14:17:04

此外,我还想出了如何更新多个文件上传,我也重构了它一点。这段代码是我的,但你明白我的意思。

代码语言:javascript
复制
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
票数 6
EN

Stack Overflow用户

发布于 2015-07-02 04:28:32

使用关联@post.post_attachments时,不需要设置post_id

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21411988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档