在我的 rails 3.2.1 应用程序中,当我尝试从我的机器上传文件时,我在浏览器中收到此错误。
NoMethodError in AssetsController#create
undefined method `symbolize_keys' for #<String:0x00000104200ad0>
app/controllers/assets_controller.rb:53:in `block in create'
app/controllers/assets_controller.rb:52:in `create
这是在我的开发机器上运行的 Rails 瘦服务器的输出:
Rendered assets/new.html.erb within layouts/application (5.8ms)
Completed 200 OK in 104ms (Views: 101.2ms | ActiveRecord: 0.5ms)
Started POST "/assets" for 127.0.0.1 at 2012-06-17 16:18:03 -0700
Served asset - 404 Not Found (7ms)
Processing by AssetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXXX=", "asset"=>{"user_id"=>"1", "uploaded_file"=>#<ActionDispatch::Http::UploadedFile:0x0000010430e080 @original_filename="41382-450x-e_37.jpeg", @content_type="image/jpeg", @headers="Content- Disposition: form-data; name=\"asset[uploaded_file]\"; filename=\"41382-450x-e_37.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/P+/P+AVMPNFEyO8F-xz7UfIP++++TI/-Tmp-/RackMultipart20120617-19438-1y1fya5>>}, "commit"=>"Create Asset"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.2ms) BEGIN
SQL (0.5ms) INSERT INTO "assets" ("created_at", "updated_at", "uploaded_file_content_type", "uploaded_file_file_name", "uploaded_file_file_size", "uploaded_file_updated_at", "user_id")
VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["created_at", Sun, 17 Jun 2012 23:18:03 UTC +00:00],
["updated_at", Sun, 17 Jun 2012 23:18:03 UTC +00:00], ["uploaded_file_content_type", "image/jpeg"], ["uploaded_file_file_name", "41382-450x-e_37.jpeg"],
["uploaded_file_file_size", 36549], ["uploaded_file_updated_at", Sun, 17 Jun 2012 23:18:03 UTC +00:00], ["user_id", 1]]
[paperclip] Saving attachments.
[paperclip] saving assets/23/41382-450x-e_37.jpeg
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 7ms
NoMethodError (undefined method `symbolize_keys' for #<String:0x00000104200ad0>):
app/controllers/assets_controller.rb:53:in `block in create'
app/controllers/assets_controller.rb:52:in `create'
这就是我在 Assets 控制器中的 create 方法的样子:
# POST /assets
# POST /assets.json
def create
@asset = current_user.assets.new(params[:asset])
respond_to do |format|
if @asset.save
format.html { redirect_to @asset, notice: 'Asset was successfully created.' }
format.json { render json: @asset, status: :created, location: @asset }
else
format.html { render action: "new" }
format.json { render json: @asset.errors, status: :unprocessable_entity }
end
end
结尾
知道会发生什么吗?非常感谢一些反馈。
谢谢
编辑:我使用 AWS gem 按照本教程将文件上传到亚马逊 s3。我的应用程序中有这个配置文件,位于 config/s3_credentials.yml,其中包含我的 amazon s3 凭据。
我在我的asset.rb 文件中使用这个代码:
has_attached_file :uploaded_file,
:path => "assets/:id/:basename.:extension",
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3_credentials.yml",
:bucket => "XXXX"
这是视图调用的我的_form:
<%= simple_form_for @asset, :html=> { :multipart => true} do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.label :UserID %>
<%= f.input :user_id %>
</div>
<div class="form-inputs">
<%= f.label :uploaded_file, "File" %>
<%= f.file_field :uploaded_file %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
s3_credential.yml 文件
development:
access_key_id:"XXXXXXXXXX"
secret_access_key:"XXXXXXXXXXXXXXXXXXXX"
production:
access_key_id:"XXXXXXXXXX"
secret_access_key:"lG/XXXXXXXXXXXXXXXXXXXX"