title: Uploading Files to db Rails 3 without using external gems
body: <p>I have a task where I need to upload a file (.txt) in Rails 3.2 without using any external gems to do the leg work(non negotiable I'm afraid) The file also needs to be saved to the database. I have the following code, however when I try to upload/create a new attachment using the form it comes back with the error;</p>
  
  <pre><code>No route matches [POST] "/attachments/create" 
  </code></pre>
  
  <p>Doesn't look like the create action is being called with uploaded_file from the model, but I'm not sure how to rectify it. If anyone can point me in the right direction it would be greatly appreciated.</p>
  
  <p>So my code is as follows; </p>
  
  <p>AttachmentsController.rb</p>
  
  <pre><code>class AttachmentsController &lt; ApplicationController
  
   def show
     @attachment = Attachment.find(params[:id])
     end_data @attachment.data, :filename =&gt; @attachment.filename, :type =&gt;  @attachment.content_type
   end
  
   def create      
     return if params[:attachment].blank?
  
     @attachment = Attachment.new
     @attachment.uploaded_file = params[:attachment]
  
     if @attachment.save
        flash[:notice] = "Thank you for your submission..."
        redirect_to root_path
     else
        flash[:error] = "There was a problem submitting your attachment."
        render "new"
       end
     end
    end
  </code></pre>
  
  <p>Attachment.rb</p>
  
  <pre><code>class Attachment &lt; ActiveRecord::Base
  
   def uploaded_file=(incoming_file)
     self.filename = incoming_file.original_filename
     self.content_type = incoming_file.content_type
     self.data = incoming_file.read
   end
  
   def filename=(new_filename)
     write_attributes("filename", sanitize_filename(new_filename))
   end
  
   private
  
   def santize_filename(filename)
     just_filename = File.basename(filename)
     just_filename.gsub(/[^\w\.\-]/, '_')
    end
   end
  </code></pre>
  
  <p>views/attachments/new.html.erb</p>
  
  <pre><code>&lt;%= form_tag('create', multipart: true) do %&gt;
    &lt;%= file_field_tag 'attachment' %&gt;
    &lt;%= submit_tag "upload", class: 'btn btn-primary' %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>routes.rb</p>
  
  <pre><code> resources :attachments
  </code></pre>
  
  <p>rake routes</p>
  
  <pre><code>    attachments GET    /attachments(.:format)          attachments#index
                  POST   /attachments(.:format)          attachments#create
   new_attachment GET    /attachments/new(.:format)      attachments#new
  edit_attachment GET    /attachments/:id/edit(.:format) attachments#edit
       attachment GET    /attachments/:id(.:format)      attachments#show
                  PUT    /attachments/:id(.:format)      attachments#update
                  DELETE /attachments/:id(.:format)      attachments#destroy
             root        /                               static_pages#home
  </code></pre>
  
  <p>Also the stack info</p>
  
  <p>Started POST "/attachments/create" for 127.0.0.1 at 2012-07-30 22:21:57 +0100</p>
  
  <pre><code>ActionController::RoutingError (No route matches [POST] "/attachments/create"):
  actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.3) lib/rails/engine.rb:479:in `call'
  railties (3.2.3) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /Users/garyrogers/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/garyrogers/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/garyrogers/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
  </code></pre>
  
  <p>Not sure where to go from here.</p>