3

我有一个任务,我需要在 Rails 3.2 中上传一个文件(.txt),而不使用任何外部 gem 来做腿部工作(恐怕不可协商)该文件还需要保存到数据库中。我有以下代码,但是当我尝试使用表单上传/创建新附件时,它会返回错误;

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

看起来没有使用模型中的 upload_file 调用创建操作,但我不确定如何纠正它。如果有人能指出我正确的方向,将不胜感激。

所以我的代码如下;

附件控制器.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

附件.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 %>

路线.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

还有堆栈信息

2012-07-30 22:21:57 +0100 开始为 127.0.0.1 发布 POST "/attachments/create"

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'

不知道从这里去哪里。

4

2 回答 2

9

而不是这样写:

<%= 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 时写的(旧版 ruby​​ 1.8.7)。:class => "btn"并且class: "btn"是相同的,实际上后者在 ruby​​ 1.9 及更高版本中更受青睐。

于 2012-07-31T03:40:07.937 回答
1

你跑了rake routes吗?像这样的资源的宁静路线可能只是一个 POST'/attachments' 看看http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

于 2012-07-30T22:33:15.053 回答