3

我的模型片段:

attr_accessible :package1_file_name, :package2_file_name
has_attached_file :package1
has_attached_file :package2

从我的 _form (简化版):

<%= form_for(@submission, :html => { :multipart => true, :id => "fileupload"  }) do |f| %>
 <%= f.file_field :package1%>
 <%= f.file_field :package2%>
<% end %>

问题是回形针将为数据库中的每个文件插入两个单独的条目。但是,我希望它只插入一个条目,因为我的表中有两个单独的字段:package1_file_name, package2_file_name.

有没有办法做到这一点?

谢谢!


对于克里斯蒂安·瓦尔加:

也许我不应该简化原始问题中的代码,但使用jQuery file upload插件后我的视图实际上是这样的:

<%= f.fields_for :uploads do |upload| %>
    <div class="row fileupload-buttonbar">
        <!-- The first upload field -->
          <span class="btn btn-success fileinput-button">
                <%= upload.file_field :package1 %>
          </span>
        <!-- The second upload field -->
          <span class="btn btn-success fileinput-button">
                <%= upload.file_field :package2 %>
          </span>
    </div>
<% end %>

upload当前模型的子模型在哪里

我不确定这是否multipart使这两个文件字段像单独的附件一样,但我正在尝试将这两个附件合并为一个。

4

1 回答 1

4

好的,所以我做了一些研究,但我仍然无法复制您的问题。我用该代码构建了一个测试应用程序,它只将一个条目插入到数据库中。

创建项目(终端)

rails new paperclip-test
cd paperclip-test
echo "gem 'paperclip'" >> Gemfile
bundle
rails generate scaffold submission
rails generate paperclip submission package1 package2
rake db:migrate

更新模型(submission.rb

attr_accessible :package1, :package2
has_attached_file :package1, :styles => { :medium => "300x300>", :thumb => "100x100>" }
has_attached_file :package2, :styles => { :medium => "300x300>", :thumb => "100x100>" }

更新控制器(submissions_controller.rb

def create
    # @submission = Submission.new(params[:submission])
    @submission = Submission.create(params[:submission])
end

更新表单(_form.html.erb

<%= f.file_field :package1 %>
<%= f.file_field :package2 %>

更新视图 ( show.html.erb )

<%= image_tag @submission.package1.url(:medium) %>
<%= image_tag @submission.package2.url(:medium) %>

运行应用程序并创建新提交

返回控制台:

sqlite3 db/development.sqlite3
select * from submissions;

结果:

1|2013-02-21 21:16:38.898602|2013-02-21 21:16:38.898602|image_1.jpg|image/jpeg|54231|2013-02-21 21:16:38.419947|image_2.jpg|image/jpeg|61766|2013-02-21 21:16:38.658720

来自https://github.com/thoughtbot/paperclip#quick-start的回形针说明

于 2013-02-21T21:38:35.080 回答