这个错误在我的Rails应用程序中意味着什么

时间:2012-06-17 23:24:13

标签: ruby-on-rails ruby amazon-s3 paperclip

在我的rails 3.2.1 app我在浏览器中遇到此错误,当我尝试从我的机​​器上传文件时。

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

知道可能会发生什么吗?非常感谢一些反馈。

由于


编辑: 我根据this教程使用AWS gem将文件上传到amazon s3。 我在app / 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 file

development:
  access_key_id:"XXXXXXXXXX"
  secret_access_key:"XXXXXXXXXXXXXXXXXXXX"

production:
  access_key_id:"XXXXXXXXXX"
  secret_access_key:"lG/XXXXXXXXXXXXXXXXXXXX"

2 个答案:

答案 0 :(得分:1)

听起来您正在尝试从传递给控制器​​的String中创建@asset。 Rails期望hashwithindifferentaccess,然后调用symbolize_keys。

答案 1 :(得分:0)

这是因为String类型没有symbolize_keys方法。此方法仅来自Hash类型。