对不起,也许是新手问题。 我想使用回形针和paperclip_database宝石来附加和保存数据库中的文件。 但我坚持将文件的数据从视图发送到控制器。 我已经完成了所有这些资源。结果我有下一个模型:
class User < ActiveRecord::Base
has_many :certificates, :dependent => :destroy
accepts_nested_attributes_for :certificates, :allow_destroy => true
end
class Certificate < ActiveRecord::Base
belongs_to :user
attr_accessor :image
has_attached_file :image,
:storage => :database,
:database_table => 'image_files',
:cascade_deletion => true
end
在控制器中
Users_controller
def new
@user = User.new
@user.certificates.build
~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~
end
def create
@user = User.new(params[:user])
~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~
end
end
我的观点形式是:
<%= form_tag @user, method: :put, :html => { :multipart => true } do |f|%>
<%= fields_for each_event_entry.certificates.first do |c| %>
<tr>
<th>Certificate</th>
<td>
<%= c.file_field :image %>
</td>
</tr>
<% end %>
<% end %>
但是当我附加文件并尝试提交时,我只有一个参数中的文件名:
{
"user" => {"some"=>"params"}
"certificate"=>{"image"=>"IMG_1642.JPG"}
}
并且证书保存在附件中。 任何帮助将不胜感激。
最后,我发现了问题所在。
看起来"form_tag"表单不需要 :html 属性,因此 html 表单应如下所示:
<%= form_tag @user, :multipart => true do |f| %>
就我而言,没有必要使用方法:也没有必要。
希望这会帮助某人。