2012-06-17 15 views
0

在我的rails 3.2.1應用程序中,當我試圖從我的機器上傳文件時,我的瀏覽器中出現此錯誤。這個錯誤在我的Rails應用程序中意味着什麼

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 

這是出來把我的機器上運行的軌道瘦服務器上:

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' 

這是我的創造方法在資產控制器的樣子:

# 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 

任何想法可能發生什麼?真的很感激一些反饋。

感謝


編輯:使用 IM的AWS寶石上傳文件到Amazon S3按照this教程。 我在config/s3_credentials.yml中包含我的亞馬遜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" 
+1

似乎應用程序正試圖訪問某種字符串給定的散列。你在使用aws寶石嗎? http://amazon.rubyforge.org/ –

+0

第52行在哪裏? – YuKagi

+0

@Yukagi:第52行是上面創建方法代碼中的行,以respond_to開始...... – banditKing

回答

1

聽起來好像您正嘗試從傳遞給控制器​​的字符串創建@asset。 Rails希望hashwithindifferentaccess,然後調用symbolize_keys。

+0

請參閱上面的編輯。 – banditKing

+1

@banditKing看過我的一些舊代碼後,我做了這個:'current_user.assets.new(params [:asset] [:uploaded_file])''。嘗試在您的創建操作中「提升params.to_yaml」以查看您是否正確訪問它。 – shanemcd

+0

我想通了。這個錯誤是由我上面代碼中的這一行引起的:「:s3_credentials =>」#{Rails.root} /config/s3_credentials.yml「,」我用 :s3_credentials => {:access_key_id =>「XXXXXXXXXX 「, :secret_access_key =>」XXXXXXXXXXXXXXXXXX「},一切正常。現在的問題是,我需要弄清楚如何處理從應用訪問AWS S3的密鑰。顯然,我不想將這些存儲在代碼中。 – banditKing

0

這是因爲字符串類型沒有symbolize_keys方法。此方法僅來自散列類型。

相關問題