2012-07-30 28 views
3

我有一个任务,我需要在Rails 3.2中上传文件(.txt),而无需使用任何外部宝石来完成腿部工作(不可谈判,我很害怕)文件也需要保存到数据库中。我有下面的代码,但是当我尝试上传/创建一个使用表单的新附件时,它会返回错误;上传文件到数据库Rails 3而不使用外部宝石

No route matches [POST] "/attachments/create" 

看起来不像create model正在使用来自模型的uploaded_file调用,但我不知道如何纠正它。如果任何人都可以指出我正确的方向,将不胜感激。

所以我的代码如下;

AttachmentsController.rb

class AttachmentsController < ApplicationController 

def show 
    @attachment = Attachment.find(params[:id]) 
    end_data @attachment.data, :filename => @attachment.filename, :type => @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 

Attachment.rb

class Attachment < 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 

视图/附件/ new.html.erb

<%= form_tag('create', multipart: true) do %> 
    <%= file_field_tag 'attachment' %> 
    <%= submit_tag "upload", class: 'btn btn-primary' %> 
<% end %> 

的routes.rb

resources :attachments 

耙路线

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 

而且堆栈信息

开始POST “/附件/创建” 为127.0.0.1在2012-07-30 22时21分57秒+0100

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' 

不知道该从哪里出发。

+0

“没有使用任何外部宝石来完成腿部工作(我不敢谈判)”<-insane(这种类型击败了使用Rails的主要优点之一)。再加上将文件存储在数据库中并不是一个好主意。 我也担心浏览器上传时间太长,因为你正在处理一个大文件,在这种情况下,你将不得不实现你自己的异步上传解决方案。从您的帖子中听到您只是信使,但如果可以的话,我会认真重新考虑要求。 – John 2012-07-31 00:16:42

回答

8

而不是写这样的:

<%= form_tag('create', multipart: true) do %> 

尝试:

<%= form_tag({:controller => 'attachment', :action => 'create'}, :multipart => true) do %> 

你可以从这里找到很多很好的例子:http://guides.rubyonrails.org/form_helpers.html

此外,你可能想改变这一行:

<%= submit_tag "upload", class: 'btn btn-primary' %> 

到:

<%= submit_tag "upload", :class => 'btn btn-primary' %> 

希望它可以帮助

更新(截至22/10/2013): 原来答案是当我刚开始学习Rails(遗留红宝石1.8.7)时写的。 :class => "btn"class: "btn"是一样的,事实上后者在Ruby 1.9和更高版本中受到青睐。

+0

嗨,卡斯佩利特,谢谢你!弗雷德我不能upvote,但是标记它是有用的。再次感谢。 – dodgerogers747 2012-07-31 09:54:00