上传文件到数据库Rails 3而不使用外部的宝石 [英] Uploading Files to db Rails 3 without using external gems
问题描述
我有一个任务,我需要在Rails 3.2中上传一个文件(.txt),而不需要使用任何外部gems来完成这个工作(我不敢担保)这个文件也需要保存到数据库中。我有下面的代码,但是当我尝试上传/创建一个新的附件使用表单时,它返回错误;
没有路由匹配[POST]/ attachments / create
看起来不像create action正在从模型的uploaded_file调用,但我不知道如何纠正它。如果有人能指出我正确的方向,将不胜感激。
所以我的代码如下:
AttachmentsController.rb
类AttachmentsController< ApplicationController
def show
@attachment = Attachment.find(params [:id])
end_data @ attachment.data,:filename => @ attachment.filename,:type => @ attachment.content_type
结束
def创建
返回如果参数[:附件] .blank?
@attachment = Attachment.new
@ attachment.uploaded_file = params [:attachment]
if @ attachment.save
flash [:notice] =谢谢您提交...
redirect_to root_path
else
flash [:error] =提交附件时出现问题。
渲染新
结束
结束
结束
Attachment.rb
class附件< ActiveRecord :: Base
$ b $ def uploaded_file =(incoming_file)
self.filename = incoming_file.original_filename
self.content_type = incoming_file.content_type $ b $ self.data = incoming_file。读
结束
def filename =(new_filename)
write_attributes(filename,sanitize_filename(new_filename))
结束
私有
$ b $ def santize_filename(filename)
just_filename = File.basename(filename)
just_filename.gsub(/ [^ \w\.\ - - ] /,'_' )
end
end
views / attachments / new.html.erb
<%= form_tag('create',multipart:true)do%>
<%= file_field_tag'附件'%>
<%= submit_tagupload,class:'btn btn-primary'%>
<%end%>
routes.rb
资源:附件
耙路线
attachments GET /attachments(.:format)attachments#index
POST /附件(.:format)attachments#create
new_attachment GET / attachments / new(。:format)附件#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 $ <$ $ p
另外栈信息
开始POST/ attachments / create对于127.0.0.1在2012-07-30 22:21:57 +0100
ActionController :: RoutingError(No r oute匹配[POST]/ attachments / create):
actionpack(3.2.3)lib / action_dispatch / middleware / debug_exceptions.rb:21:在`call'
actionpack(3.2.3) /action_dispatch/middleware/show_exceptions.rb:56:in`call'
railties(3.2.3)lib / rails / rack / logger.rb:26:在`call_app'
railties(3.2.3 )lib / rails / rack / logger.rb:16:在`call'
actionpack(3.2.3)lib / action_dispatch / middleware / request_id.rb:22:在`call'
rack(1.4 .1)lib / rack / methodoverride.rb:21:在`call'
rack(1.4.1)lib / rack / runtime.rb:17:在`call'
activesupport(3.2.3 )lib / active_support / cache / strategy / local_cache.rb:72:在`call'
rack(1.4.1)lib / rack / lock.rb:15:在`call'
actionpack(3.2 .3)lib / action_dispatch / middleware / static.rb:62:在`call'
railties(3.2.3)lib / rails / engine.rb:479:在`call'
railties(3.2 (3)lib / rails / application.rb:220:在`call'
rack(1.4.1)lib / rack / content_length.rb:14:`call'
railties(3.2.3 )lib / rails / rack / log_tailer.rb :14:在`call'
rack(1.4.1)lib / rack / handler / webrick.rb:59:在`service'
/Users/garyrogers/.rvm/rubies/ruby-1.9 .3-p0 / lib / ruby / 1.9.1 / webrick / httpserver.rb:138:在'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:在`block in start_thread'
不知道该从哪里出发。
解决方案
<%= form_tag('create',multipart:true)do%>
尝试:
<%= form_tag({:controller =>'attachment',:action =>'create'},:multipart => true)do%>
你可以从这里找到很多很好的例子: http://guides.rubyonrails.org/form_helpers.html
另外,你可能要改变这一行:
<%= submit_tagupload,class:'btn btn-primary'%> ; b
$ b code><%= submit_tagupload,:class => 'btn btn-primary'%>
希望有帮助
更新(截至2013年10月22日):
原来的答案是我刚开始学习Rails(legacy ruby 1.8.7)时编写的。 :class => btn
和 class:btn
是一样的,事实上后者在Ruby 1.9和更高版本中是受青睐的。 b $ bI 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;
No route matches [POST] "/attachments/create"
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.
So my code is as follows;
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
views/attachments/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
rake routes
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
Also the stack info
Started POST "/attachments/create" for 127.0.0.1 at 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'
Not sure where to go from here.
解决方案 Instead of writing like this:
<%= form_tag('create', multipart: true) do %>
Try:
<%= form_tag({:controller => 'attachment', :action => 'create'}, :multipart => true) do %>
You can find a lot of good examples from here:http://guides.rubyonrails.org/form_helpers.html
Besides, you probably want to change this line:
<%= submit_tag "upload", class: 'btn btn-primary' %>
to:
<%= submit_tag "upload", :class => 'btn btn-primary' %>
Hope it helps
Update(as of 22/10/2013):
The original answer was written when I just started learning Rails(legacy ruby 1.8.7). :class => "btn"
and class: "btn"
are the same, in fact the latter is favoured in ruby 1.9 and above.
这篇关于上传文件到数据库Rails 3而不使用外部的宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!