我在Rails和Web開發新...回形針不保存附件
我創建了一個用戶模式,現在我想給用戶添加一個資料圖片,使用回形針的能力。
在我的用戶顯示頁面中,用戶可以點擊鏈接打開「編輯」頁面,從中可以看到表單以瀏覽並選擇要上傳的圖像。點擊按鈕時,它會調用「更新」操作並重定向到用戶顯示頁面,但圖像不會保存在任何文件夾中,並且圖像屬性(文件名,內容類型,文件大小)在數據庫中仍設置爲NIL 。
- 我安裝並測試了ImageMagick的
- 我加入的形式
:multipart => true
- 我已經把attr_accessible:頭像
- 我已經設置了回形針選項尋找「在/ usr/bin中/ '其中轉換位於
- 我已經運行遷移
- 我已設置:url和:路徑
-in控制器,我的更新操作是:
def update
@user = User.find(params[:id])
@title = "Update profile picture"
response_to do |format|
if @user.update_attributes(params[:user])
format.html {redirect_to(@user, :notice => 'Profile picture loaded')}
else
format.html {render :action => "edit", :notice => 'Unable to load pic")}
end
end
end
我的模型代碼是:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :number_of_positive_reco, :confidence_percent, :password,
:password_confirmation, :avatar, :avatar_file_name, :avatar_content_file, :avatar_file_size
has_attached_file :avatar , :styles => { :medium => "300x300>", :thumb => "100x100>"},
:url => "/images/profiles/:attachment/:id_:style.:extension",
:path => ":rails_root/public/images/profiles/:attachment/:id_:style.:extension"
# :default_url => "/images/Default_profile_picture.png"
email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 20}
validates :email, :presence => true,
:format => { :with => email_regex},
:uniqueness => {:case_sensitive => false}
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
validates :number_of_positive_reco, :numericality => {:only_integer => true, :greater_than_or_equal_to => 0}
validates :confidence_percent, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 1.0}
before_save :encrypt_password
# Return true if the user's password matches the submitted password.
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil?
return user if user.has_password?(submitted_password)
end
def self.authenticate_with_salt(id, cookie_salt)
user = find_by_id(id)
(user && user.salt == cookie_salt) ? user : nil
end
private
def encrypt_password
self.salt = make_salt if new_record?
self.encrypted_password = encrypt(password)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
end
的形式位於edit.html.erb:
<h1>
Ajouter une photo au profil
</h1>
<%= form_for @user, :html => { :multipart => true} do |f| %>
<div class="field">
<%= f.label :avatar, "Upload ta photo" %>
<br />
<%= f.file_field :avatar %>
</div>
<div class="actions">
<%= f.submit "Upload" %>
</div>
<% end %>
而且我將調試信息打印到瀏覽器中。點擊上傳後,我得到這個:
{"commit"=>"Upload", "authenticity_token"=>"+ExcuQOSv1bxIyAoM5+N4TCSmYI8JYeh5Yb8P5W4VU0=", "_method"=>"put", "utf8"=>"✓", "action"=>"update", "id"=>"8", "controller"=>"users", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0xb6d63fec @content_type="image/jpeg", @original_filename="Paperclip-Railway.jpg", @tempfile=#<File:/tmp/RackMultipart20111208-1681-3h3ps4-0>, @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"Paperclip-Railway.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
所以,在日誌中,我看到,「曲別針的字段」都充滿了形象的名字,圖像類型,等等......但沒有「INSERT INTO TABLE 「,所有用戶字段仍然是NIL,沒有創建用戶映像應存儲的系統目錄,沒有」Paperclip attachment save「,也沒有提及日誌中的回形針...
在控制檯模式下,我可以創建一個新用戶,將頭像屬性設置爲:
`User.create(:avatar => File.new(Rails.root +「public/images/an_image.png」)'
它工作得很好! ...我還測試了一個新的文件夾的創建,如果沒有管理員權限,和它的作品都很好......我desesperate :-(
誰能幫我?
你可以發佈你的模型代碼嗎? – ramblex
@ramblex:上面發佈了模型,表單和調試信息。謝謝回答 ! – citraL