Ruby on rails 使用回形针上传图像+;轨道。如何将图像保存到与上载模型不同的模型

Ruby on rails 使用回形针上传图像+;轨道。如何将图像保存到与上载模型不同的模型,ruby-on-rails,ruby,file-upload,paperclip,Ruby On Rails,Ruby,File Upload,Paperclip,我有一个模型叫做列表。在my index.html.erb中查找列表。我在屏幕上有所有的列表。清单模型的_form.html.erb如下所示: <%if user_signed_in? %> <%=link_to "Sign Out", destroy_user_session_path, :method => :delete %><br/> <%= form_for(@listing, :html => {:multipart =>

我有一个模型叫做列表。在my index.html.erb中查找列表。我在屏幕上有所有的列表。清单模型的_form.html.erb如下所示:

<%if user_signed_in? %>
<%=link_to "Sign Out",  destroy_user_session_path, :method => :delete %><br/>
<%= form_for(@listing, :html => {:multipart => true} ) do |f| %>
<% if @listing.errors.any? %>
 <div id="error_explanation">
   <h2><%= pluralize(@listing.errors.count, "error") %> prohibited this listing from   being saved:</h2>
  <ul>
  <% @listing.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
  </div>
 <% end %>
 <div class="field">
 <%= f.label :name %><br />
 <%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :id %><br />
<%= f.text_field :id %>
</div>

:删除%>
{:multipart=>true})do | f |%> 禁止保存此列表:




联系管理员以获取登录名和密码

在这里,与此视图关联的模型是“列表”,但我想将上传的图像属性存储在应用程序中的另一个模型中:“图像”

基本上,我想做的是,让我所有的模特通过他们的视图上传他们自己的照片,但是把所有的图片属性保存在一个叫做“图片”的模型中。图像本身存储在AmazonS3上

所以问题是“列表”模型中的哪些代码实现了这一点,以及“图像”模型中的哪些代码实现了这一点。这些信息是如何从列表传递到图像的

当前在列表模型中:

class Listing < ActiveRecord::Base

 has_attached_file :image,:styles => {:thumb => "75x75>", :small => "150x150>", :medium => "300x300>"}

end
类列表{:thumb=>“75x75>”,:small=>“150x150>”,:medium=>“300x300>”}
结束
目前处于“图像”模式

#==架构信息
#
#表名:图像
#
#id:整数不为空,主键
#图像文件名称:字符串(255)
#图像内容类型:字符串(255)
#图像\文件\大小:整数
#图像类型:字符串(255)
#创建时间:datetime非空
#更新时间:datetime非空
#
类映像

提前感谢您的帮助。

我假设列表和图像之间存在关系,但您明白了

<% fields_for @listing.image do | img | %>
  <%= img.file_field :image %>
  #etc
  <% end %>
<% end %>
<% fields_for @listing.image do | img | %>
  <%= img.file_field :image %>
  #etc
  <% end %>
<% end %>

#等

当然,如果您不希望它们相互关联,您可以创建一个新的图像对象并将其传递给字段_,但保持它们的关联是有意义的

简单地说,以下是您在模型中的内容:

上市模式

class Listing < ActiveRecord::Base
  has_one :image, dependent => :destroy
end
类列表:destroy
结束
图像模型

class Image < ActiveRecord::Base
  has_attached_file :image,
                    :styles => {:thumb => "75x75>", 
                                :small => "150x150>", 
                                :medium => "300x300>"}
end
类映像{:thumb=>“75x75>”,
:small=>“150x150>”,
:medium=>“300x300>”}
结束
然后在您的表格中(来自DVG的答案):


#等
<% fields_for @listing.image do | img | %>
  <%= img.file_field :image %>
  #etc
  <% end %>
<% end %>