Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoMethodError (undefined method `file_identifier' for #<Document:0x27117a8>): #2

Closed
ghost opened this issue Jul 31, 2012 · 1 comment

Comments

@ghost
Copy link

ghost commented Jul 31, 2012

Hi,

I get NoMethodError (undefined method 'file_identifier' for #<Document:0x27117a8>) when I try to save a form with attachement.

new.haml

= simple_form_for @document, :remote => true, :novalidate => true do |form|
  %p#errors{:style => 'display:none'}

  %p
    Title
    = form.text_field :title, :required => true

  %p
    Description
    = form.text_field :description, :required => true

  %p
    File to upload
    = form.file_field :file, id: 'file'

  .clear
  .action_bar
    %input.button.blue.small{type: "submit", value: "Submit"}
    %a.button.red.small
      %span.glyph.cross
      Annuler

document.rb (model)

class Document < ActiveRecord::Base
  attr_accessible :description, :file, :title

  validates :title, :description, presence: true
  validates_attached_presence :file

  has_attached :file
end

schema

  create_table "documents", :force => true do |t|
    t.string   "title"
    t.text     "description"
    t.integer  "company_id"
    t.datetime "created_at",        :null => false
    t.datetime "updated_at",        :null => false
    t.string   "file_file_name"
    t.string   "file_content_type"
    t.integer  "file_file_size"
    t.datetime "file_updated_at"
  end

POST REQUEST

Started POST "/documents" for 127.0.0.1 at 2012-07-31 15:09:50 +0200
Processing by DocumentsController#create as JS
  Parameters: {"utf8"=>"V", "authenticity_token"=>"DVrcEoFETW+iohGHFa1GdvRPjnmGbcaGpiWhLnZbXOc=", "document"=>{"title"=
"dd", "description"=>"dd"}}
  ←[1m←[35mUser Load (0.0ms)←[0m  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 3]]
  ←[1m←[36mCompany Load (0.0ms)←[0m  ←[1mSELECT "companies".* FROM "companies" WHERE "companies"."id" = 3 LIMIT 1←[0m
Completed 500 Internal Server Error in 4ms

NoMethodError (undefined method `file_identifier' for #<Document:0x27117a8>):
  app/controllers/documents_controller.rb:30:in `create'


  Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.7/lib/action_dispatch/middleware/templates/rescues/_trace
erb (4.0ms)
  Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.7/lib/action_dispatch/middleware/templates/rescues/_reque
t_and_response.erb (2.0ms)
  Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.7/lib/action_dispatch/middleware/templates/rescues/diagno
tics.erb within rescues/layout (36.0ms)

documents_controller

  def create
    ....
    ....

    @document =Document.new(params[:document])

    if @document.valid?
      @document.save
    end

    ....
    ....
  end

Thank you.

@ksylvest
Copy link
Owner

ksylvest commented Aug 1, 2012

Hi Yassine.

Thanks for the detailed overview. The error is occurring because your database doesn't have the correct columns. In order for attached to work you need: 'file_identifier' (string), 'file_extension' (string) and 'file_size' (integer). Substitute the name of the file within the application for 'file' (example: 'avatar_identifier', 'avatar_extension' and 'avatar_size'). In your migration you can use the 'attachment' type to generate all three columns at once (see read me). If this doesn't fix your problem feel free to reopen the issue!

@ksylvest ksylvest closed this as completed Aug 1, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant