編輯:獲取:使用多個Paperclip附件保存模型時未定義的方法`stringify_keys'。 (ROR,回形針,S3)
:s3_credentials => { :access_key_id => 'AAAAAAAAAOKSSISVNQ',
:secret_access_key => 'AAAAAAAAAAAAAAAAAAA1eWYh0au8Pg4bOnAmmX' ,
:bucket => 'my_bucket_name' },
一個新的問題現在出現,上傳圖像時:下面的問題是通過直接包括S3憑據的回形針模型(資產),這樣的解決到S3: ** AWS ::核心::客戶:: NetworkError ** 應用程序/控制器/用戶/ registrations_controller.rb:9:'更新」
這種情況發生在我的本地Windows 7計算機上,護欄服務器,試圖上傳到我的S3存儲桶。
原始發行日期:
我試圖用回形針上傳多張圖片(資產),並保存與控制器更新動作我的用戶模型一起。 我跟着這個回形針S3 Totorial:http://webtempest.com/how-to-allow-image-uploads-in-rails-on-heroku/
我發現了10個左右不同的答案與回形針這個確切的錯誤,但沒有一個解決了我的問題。
如果我從我的資產模型註釋掉S3配置行,錯誤消失。
在我registration_controller更新操作發生錯誤:
**undefined method `stringify_keys' for #<String:0x2a522d8>
app/controllers/users/registrations_controller.rb:8:in `update'**
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"ACFzngW4IXpuUDXutwHDCbpCuTjx2sFrZpcwBqt31LU=",
"user"=>{"nickname"=>"newuser1",
"first_name"=>"111",
"last_name"=>"222",
"birth_date"=>"2012-01-01 00:00:00.000000",
"email"=>"[email protected]",
"assets_attributes"=>{"0"=>{"asset"=>#<ActionDispatch::Http::UploadedFile:0x2a97a70 @original_filename="CIMG6275.JPG",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"user[assets_attributes][0][asset]\"; <br/>filename=\"CIMG6275.JPG\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:C:/Users/AA/AppData/Local/Temp/RackMultipart20120905-7504-7sivk1>>}},
"instrument_ids"=>["1",
"2",
"3"],
"id"=>"9"},
"commit"=>"Update"}
這裏的事項我的用戶模型的一部分:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :id, :email, :password, :password_confirmation, :remember_me,
first_name, :last_name, :birth_date, :nickname, :instrument_ids, :assets,:assets_attributes
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
...
的資產模型(這是附件回形針對象):
class Asset < ActiveRecord::Base
belongs_to :user
has_attached_file :asset,
:whiny => false,
:styles => { :large=>"640x480g", :medium => "300x300>", :thumb => "100x100>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'name_of_my_bucket'
end
我的控制器更新代碼:
class Users::RegistrationsController < Devise::RegistrationsController
public
def update
params[:user][:instrument_ids] ||= []
@user = User.find(params[:user][:id])
if @user.update_without_password(params[:user])
respond_to do |format|
format.html { redirect_to root_path }
format.xml { head :ok }
end
else
respond_to do |format|
format.html { render :action => "edit", :layout => "dialog" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
...
用戶認爲重要的部分:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :multipart => true }) do |f| %>
<%= devise_error_messages! %>
<% counter = 1 %>
<%= f.fields_for :assets do |asset_fields| %>
<% if asset_fields.object.new_record? %>
<%= f.label "Image #{counter}" %>
<% counter = counter + 1 %>
<%= asset_fields.file_field :asset %>
<%= asset_fields.label :asset_file_name %>
<% end %>
<% end %>
在schema.rb文件的資產模型:
create_table "assets", :force => true do |t|
t.string "asset_file_name"
t.string "asset_content_type"
t.integer "asset_file_size"
t.datetime "asset_updated_at"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
最後,Gemfile中的一部分:
gem 'jquery-rails'
gem 'aws-s3'
gem 'aws-sdk'
gem 'devise'
gem 'cancan'
gem 'omniauth-facebook'
gem 'omniauth'
gem 'roo'
gem 'client_side_validations'
gem 'sqlite3'
gem 'nifty-generators'
gem 'paperclip'
s3.yml: access_key_id:AKUAJO4RGQ4TKSSIQVNB
secret_access_key:UGiDBv2rohLJdIHNSQK3N1eWYh0au8Pg4bOnAxxY
鬥:my_bucket_name
我修改了上面的幾個字符只是爲了安全起見
在此先感謝,亞歷克斯
請提供 「#{} Rails.root /config/s3.yml」 – sumskyi
access_key_id:AKUAJO4RGQ4TKSSIQVNB secret_access_key:UGiDBv2rohLJdIHNSQK3N1eWYh0au8Pg4bOnAxxY
鬥:my_bucket_name
#(爲了安全起見,我在上面修改了幾個字符) – Alex
你解決了你的問題嗎?我有類似的錯誤! – Azzurrio