【发布时间】:2017-01-14 20:15:34
【问题描述】:
在 Ruby on Rails 上使用回形针 gem 设置多态文件上传时出错。
下面,我将向您展示相关的代码部分。
我已经设置了一个 _form 部分:
<%= form_for(@test_run, :html => { :multipart => true }) do |f| %>
<div class="row">
<div class="large-6 columns">
<%= fields_for :attachment do |attachment_fields| %>
<%= attachment_fields.file_field :attachment %><br />
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit t('buttons.general.save'), :class => "button slim", data: {disable_with: "#{t('buttons.general.disable')}"}, :alt => t('buttons.general.save'),:title => t('buttons.general.save') %>
</div>
</div>
我的附件模型包含文件验证:
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => :true
has_attached_file :attachment
validates_attachment_content_type :attachment, content_type: /\A(image|application)\/.*\z/
end
在附件控制器中,我将以下参数列入白名单:
def attachment_params
params.require(:attachment).permit(:name, :user_id, :attachable_id, :attachable_type, :attachedfile)
end
附件应附加到测试运行。这是测试运行模型:
class TestRun < ActiveRecord::Base
belongs_to :test_object
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments
end
测试运行控制器的创建方法和私有方法也可能相关:
def create
@test_run = TestRun.new(test_run_params)
attachment = Attachment.create(:attachable_id => @test_run_id, :attachment => params[:attachment])
render text: @test_run.attachments.first.inspect
end
private
# Use callbacks to share common setup or constraints between actions.
def set_test_run
@test_run = TestRun.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def test_run_params
params.require(:test_run).permit(:name, :test_object_id, :attachment, :start_time, :end_time, :test_demands, :allowed_bug_types, :max_testers, :test_language, :postprocessing_time, :test_standards, :target_tester, :target_devices)
end
为了给你一个完整的概述,这里是附件迁移文件:
class CreateAttachments < ActiveRecord::Migration
def change
create_table :attachments do |t|
t.string :name
t.integer :user_id
t.integer :attachable_id
t.string :attachable_type
t.timestamps null: false
end
end
end
现在,当我运行此程序时,填写表单并选择一个有效文件并在此之后提交表单,我收到以下错误:
TestRunsController 中的Paperclip::AdapterRegistry::NoHandlerError #create
好吧,不知何故,stackoverflow 缩小了错误信息,所以我把它放在pastebin 上。
如果希望,你们中的某个人可以帮助我,因为我在研究错误的日子以来一直在这一点上。
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby web file-upload paperclip