在 Ruby on Rails 中使用 Paperclip 进行多文件上传

3
我正在使用Paperclip上传建筑的照片。这个视频链接http://www.youtube.com/watch?v=KGmsaXhIdjc展示了如何完成单张照片上传。但是,现在我想要上传多张照片到同一个建筑中。我能否继续使用Paperclip实现呢?或者必须改用jQuery?如果可以的话,应该怎么做呢?P.S:如果需要,我可以上传我的代码。P.S:我正在考虑在数据库中添加2列photo2和photo3。。此外,我已经看到所有生成的资产来完成这个过程。问题是我已经通过不同的方式上传了一张照片。这意味着我需要改变整个流程吗?
更新1:
在routes.rb中:
resources :buildings do 
      resources :photos
  end

在建筑物中>_表单

<%= form_for(@building, :html=>{:multipart => true}) do |f| %>
  <% if @building.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@building.errors.count, "error") %> prohibited this building from being saved:</h2>

      <ul>
      <% @building.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :status %><br />
    <%= f.select :status, Building::STATUS, :prompt=>'Select status of the building' %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description, :rows => 10 %>
  </div>
  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </div>
  <div class="field">
    <!--<%= f.label :photo %><br />
    <%= f.file_field :photo %> -->
    <%= f.fields_for :photos do |photo| %>  
    <% if photo.object.new_record? %>
      <%= photo.file_field(:image) %>

     <% else %> 
        <%= image_tag(photo.url(:thumb)) %>
        <%= photo.hidden_field :_destroy %>
        <%= photo.link_to_remove "X"%>
    <% end %>
  <% end %>

    <p><%= f.link_to_add "Add photo", :photos %></p>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

在模型构建中

 attr_accessible :description, :price, :status, :title, :photo

accepts_nested_attributes_for :photo, :allow_destroy => true

  has_attached_file :photo ,
                  :url  => "/assets/products/:id/:style/:basename.:extension",
                  :path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"

1
请查看https://dev59.com/lWgu5IYBdhLWcg3w_cBV#10922612和http://www.emersonlackey.com/article/rails-paperclip-multiple-file-uploads。 - Prakash Murthy
我已经尝试过了,但没有成功。也许是我做错了什么,我不知道。 - marios
1个回答

5

您可以使用Paperclip进行此操作。我使用嵌套资源和nested_form gem实现。

例如,您的building has_many :photosphoto belongs_to :building

然后在您的/views/buildings/_form.html.erb中,您可以编写以下内容:

<%= nested_form_for @building, :html => { :multipart => true } do |f| %>
  <%# all your building fields .... %>

  <%= f.fields_for :photos do |photo| %>  
    <% if photo.object.new_record? %>
      <%= photo.file_field(:image) %>
    <% else %> 
      <%= image_tag(photo.url(:thumb)) %>
      <%= photo.hidden_field :_destroy %>
      <%= photo.link_to_remove "X" %>
    <% end %>
  <% end %>

  <p><%= f.link_to_add "Add photo", :photos %></p>

  <%= f.submit %>
<% end %>

您需要在您的building.rb模型中设置accepts_nested_attributes_for :photos, :allow_destroy => true,并确保您的routes.rb也包含了嵌套:
resources :buildings do 
  resources :photos
end

我已经做了一切,但是出现了以下错误信息: BuildingsController#new 中的 ArgumentError找不到名称为“photos”的关联。它是否已经被定义? - marios
有时候会提示我:BuildingsController#new中出现NoMethodErrornil:NilClass没有定义'key?'方法 - marios

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接