2016-09-07 109 views
0


我在使用Ruby on Rails上的回形針gem設置多態文件上傳時出錯。
在下面,我會告訴你相關的代碼部分。
Ruby on Rails - 回形針 - NoHandleError

我已經成立了一個局部_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 

現在,當我運行此,填寫表格,並選擇一個有效的文件,並提交形式在此之後,我收到以下錯誤:

TestCloud :: AdapterRegistry :: NoHandlerError TestRunsController#create
好吧,不知何故,stackoverflow收縮錯誤消息,所以我把它放在pastebin

如果希望,你們中的某個人可以幫助我,因爲自從研究bug以來,我就處於這一點。
謝謝!

回答

1

運行遷移:

rails generate paperclip attachment attachment 
在你看來

<%= fields_for :attachment do |field| %> 
    <%= field.file_field :attachment %><br /> 
<% end %> 

attachment_params

def attachment_params 
    params.require(:attachment).permit(:name, :user_id, :attachable_id, :attachable_type, :attachment) 
end 

你的方法create

def create 

    @test_run = TestRun.new(test_run_params) 

    attachment = Attachment.create(attachment_params) 

    render text: @test_run.attachments.first.inspect 
end 

您還必須包含在此控制器方法中attachment_params

+0

謝謝您的回答,但這會導致相同的錯誤。 –

+0

你還可以顯示你的'TestRunController'中的代碼嗎? –

+0

我已將它添加到問題中。只有創建方法和私有方法應該是相關的,因爲在創建測試運行時發生錯誤。 –

相關問題