Leo V Leo V - 1 year ago 96
Ruby Question

Ruby on Rails - Paperclip - NoHandleError

I got an error while setting up a polymorphic file upload with the paperclip gem on Ruby on Rails.

In the following, I will show you the relevant code parts.

I've set up a _form partial:

<%= 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') %>

My attachment model contains the file validation:

class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => :true

has_attached_file :attachment
validates_attachment_content_type :attachment, content_type: /\A(image|application)\/.*\z/

In the attachment controller I whitelist the following parameters:

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

The attachment should be attached to a test run. Here is the test run model:

class TestRun < ActiveRecord::Base
belongs_to :test_object

has_many :attachments, :as => :attachable

accepts_nested_attributes_for :attachments

The create method and the private methods of the test runs controller could be relevant, too:

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

# Use callbacks to share common setup or constraints between actions.
def set_test_run
@test_run = TestRun.find(params[:id])

# 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)

To give you a completely overview, here is the attachment migration file:

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

Now, when I run this, fill out the form and select a valid file and submit the form after this, I receive the following error:

Paperclip::AdapterRegistry::NoHandlerError in TestRunsController#create

Ok, somehow, stackoverflow shrinks the error message, so I put it on pastebin.

If hope, someone of you could help me, as I am on this point since days of researching for bugs.

Thank you!

Answer Source

run migration:

rails generate paperclip attachment attachment

in your view:

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

your attachment_params

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

your method create

def create

  @test_run = TestRun.new(test_run_params)

  attachment = Attachment.create(attachment_params)

  render text: @test_run.attachments.first.inspect

you must also included in this controller method attachment_params

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download