gpt4 book ai didi

jquery - ror 中使用 Paperclip 进行多次上传

转载 作者:行者123 更新时间:2023-12-01 01:01:57 24 4
gpt4 key购买 nike

我正在使用回形针上传一栋建筑物的一张照片。 http://www.youtube.com/watch?v=KGmsaXhIdjc我已经用这种方法做到了。但我决定将多张照片上传到一栋建筑。我可以使用回形针来做到这一点还是必须更改它并使用 jQuery ?如果我可以怎么办? Ps:如果需要,我会上传我的代码。 PS:我正在考虑在数据库中为照片2和照片3再创建2列..顺便说一句,我已经看到所有生成的 Assets 都可以做到这一点。我的想法是,我用不同的方式上传一张照片。这意味着我必须改变一切?

更新1:

在routes.rb中

resources :buildings do 
resources :photos
end

在建筑物中>_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 %>

在模型>建筑物

 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 。

例如,您building has_many :photosphoto own_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

关于jquery - ror 中使用 Paperclip 进行多次上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14880100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com