多次上载Paperclip进行上传 [英] Multiple upload with Paperclip in ror

查看:85
本文介绍了多次上载Paperclip进行上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用回形针为一栋建筑物上传一张照片. http://www.youtube.com/watch?v=KGmsaXhIdjc 我已经完成了用这种方式.但知道我决定将许多照片上传到一栋建筑.我可以使用回形针做到这一点,还是必须对其进行更改并使用jQuery?如果我能怎么办? 附言:如果需要,我将上传我的代码. ps:我正在考虑在数据库中为photo2和photo 3再增加2列..顺便说一句,我已经看到所有生成此操作的资产.我以为我已经用不同的方式上传了1张照片.这意味着我必须改变所有事情吗?

I am using paperclip to upload one photo for one building. http://www.youtube.com/watch?v=KGmsaXhIdjc i have done it with this way. But know I decide to upload many photos to one building. Can I do that with paperclip or have to change it and use jQuery ? And if I can how ? Ps: if needed I will upload my code. ps: i am thinking to make 2 more columns in database for photo2 and photo 3.. btw i have see all generates assets to do that. the think is that i have make it with different way to upload 1 photo. this means i have to change the all thing?

更新1:

在routes.rb中

in routes.rb

resources :buildings do 
      resources :photos
  end

在建筑物中> _form

in buldings>_form

<%= 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 %>

在model>建筑物中

in model>building

 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"

推荐答案

是的,您可以使用回形针进行操作.我这样做的方法是使用嵌套资源和 nested_form gem.

Yes you can do it with paperclip. The way I do it is with nested resources and the nested_form gem.

例如您building has_many :photosphoto belongs_to :building

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

Then in your /views/buildings/_form.html.erb you would write something like this:

<%= 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还包括嵌套:

You will have to set accepts_nested_attributes_for :photos, :allow_destroy => true inside your building.rb model and make sure your routes.rb also include the nesting:

resources :buildings do 
  resources :photos
end

这篇关于多次上载Paperclip进行上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆