我已设置Carrierwave将pdf文件上传到我的数据库。我在运行bundle install后使用我的Gemfile进行安装。问题是,当我尝试在数据库中创建一个新对象时,出现错误:
TypeError in BulletinsController#create
can't cast ActionDispatch::Http::UploadedFile to string
我相信我正确地设置了所有内容,但我将不胜感激。这是我的班级/模特/控制员。
AttachmentUploader:
class AttachmentUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
公告模型:
class Bulletin < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
end
公告控制器:
class BulletinsController < ApplicationController
before_action :set_bulletin, only: [:show, :edit, :update, :destroy]
# GET /bulletins
# GET /bulletins.json
def index
@bulletins = Bulletin.all
end
# GET /bulletins/1
# GET /bulletins/1.json
def show
end
# GET /bulletins/new
def new
@bulletin = Bulletin.new
end
# GET /bulletins/1/edit
def edit
end
# POST /bulletins
# POST /bulletins.json
def create
@bulletin = Bulletin.new(bulletin_params)
respond_to do |format|
if @bulletin.save
format.html { redirect_to @bulletin, notice: 'Bulletin was successfully created.' }
format.json { render :show, status: :created, location: @bulletin }
else
format.html { render :new }
format.json { render json: @bulletin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /bulletins/1
# PATCH/PUT /bulletins/1.json
def update
respond_to do |format|
if @bulletin.update(bulletin_params)
format.html { redirect_to @bulletin, notice: 'Bulletin was successfully updated.' }
format.json { render :show, status: :ok, location: @bulletin }
else
format.html { render :edit }
format.json { render json: @bulletin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /bulletins/1
# DELETE /bulletins/1.json
def destroy
@bulletin.destroy
respond_to do |format|
format.html { redirect_to bulletins_url, notice: 'Bulletin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_bulletin
@bulletin = Bulletin.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def bulletin_params
params.require(:bulletin).permit(:title, :text, :attachment)
end
end
最后是公告new.html表格:
<%= form_for @bulletin, :html => {:multipart => true} do |f| %>
<% if @bulletin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@bulletin.errors.count, "error") %> prohibited this bulletin from being saved:</h2>
<ul>
<% @bulletin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :text %><br>
<%= f.text_area :text %>
</div>
<div class="field">
<%= f.label :attachment %><br>
<%= f.file_field :attachment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
完全追踪:
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/quoting.rb:76:in `type_cast'
activerecord (4.1.1) lib/active_record/connection_adapters/sqlite3_adapter.rb:261:in `type_cast'
activerecord (4.1.1) lib/active_record/connection_adapters/sqlite3_adapter.rb:295:in `block in exec_query'
activerecord (4.1.1) lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `map'
activerecord (4.1.1) lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `exec_query'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:78:in `exec_insert'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:105:in `insert'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
activerecord (4.1.1) lib/active_record/relation.rb:64:in `insert'
activerecord (4.1.1) lib/active_record/persistence.rb:502:in `create_record'
activerecord (4.1.1) lib/active_record/attribute_methods/dirty.rb:87:in `create_record'
activerecord (4.1.1) lib/active_record/callbacks.rb:306:in `block in create_record'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
activerecord (4.1.1) lib/active_record/callbacks.rb:306:in `create_record'
activerecord (4.1.1) lib/active_record/timestamp.rb:57:in `create_record'
activerecord (4.1.1) lib/active_record/persistence.rb:482:in `create_or_update'
activerecord (4.1.1) lib/active_record/callbacks.rb:302:in `block in create_or_update'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
activerecord (4.1.1) lib/active_record/callbacks.rb:302:in `create_or_update'
activerecord (4.1.1) lib/active_record/persistence.rb:103:in `save'
activerecord (4.1.1) lib/active_record/validations.rb:51:in `save'
activerecord (4.1.1) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (4.1.1) lib/active_record/transactions.rb:268:in `block (2 levels) in save'
activerecord (4.1.1) lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `block in transaction'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
activerecord (4.1.1) lib/active_record/transactions.rb:208:in `transaction'
activerecord (4.1.1) lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
activerecord (4.1.1) lib/active_record/transactions.rb:268:in `block in save'
activerecord (4.1.1) lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
activerecord (4.1.1) lib/active_record/transactions.rb:267:in `save'
app/controllers/bulletins_controller.rb:30:in `block in create'
actionpack (4.1.1) lib/action_controller/metal/mime_responds.rb:433:in `call'
actionpack (4.1.1) lib/action_controller/metal/mime_responds.rb:433:in `retrieve_collector_from_mimes'
actionpack (4.1.1) lib/action_controller/metal/mime_responds.rb:256:in `respond_to'
app/controllers/bulletins_controller.rb:29:in `create'
actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.1) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.1) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.1) lib/rails/engine.rb:514:in `call'
railties (4.1.1) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/usr/local/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/usr/local/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/usr/local/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"hCh2yJkRxNKW+z2ZOIISuAWu/2gRI++noacQxDnst78=",
"bulletin"=>{"title"=>"Test",
"text"=>"Testing123",
"attachment"=>#<ActionDispatch::Http::UploadedFile:0xc69d9b0 @tempfile=#<Tempfile:/tmp/RackMultipart20140619-13131-17crugy>,
@original_filename="test.pdf",
@content_type="application/pdf",
@headers="Content-Disposition: form-data; name=\"bulletin[attachment]\"; filename=\"test.pdf\"\r\nContent-Type: application/pdf\r\n">},
"commit"=>"Create Bulletin"