2010-12-11 25 views
1

我已經得到回形針在我的系統上工作沒有問題。在我剛剛構建的這個最新應用程序中,出於某種原因,我無法使用回形針來保存圖像,即使日誌文件顯示它是,它也不會顯示在sql數據中。Ruby 1.87 Rails 3.03和Devise。回形針圖像沒有被保存,雖然它說它在日誌文件中

我唯一的區別就是使用Devise,並想知道是否有一些額外的設置需要改變。

日誌文件表示:

Started POST "https://stackoverflow.com/users/1" for 127.0.0.1 at Fri Dec 10 14:56:52 -0800 2010 

處理由UsersController#更新爲HTML 參數:{ 「提交」=> 「更新」, 「authenticity_token」=> 「JexxammsT + VPzqwbLr/YohuY4vcpBFdfVrOwDjDQEUo =」, 「utf8」=>「✓」,「id」=>「1」,「user」=> {「avatar」=>#,@ headers =「Content-Disposition:form-data; name = \」user [avatar ] \「; filename = \」Nice-Smile.jpg \「\ r \ nContent-Type:image/jpeg \ r \ n」,@ content_type =「image/jpeg」,@ original_filename =「Nice-Smile.jpg」 (「users」,「id」= 1)限制1 用戶負載(0.5ms)SELECT「users」。「id」FROM 「users」WHERE(「users」。「email」='mrus (「users」.id <> 1)限制1 [回形針]保存附件。 重定向到124ms

實測值http://localhost:3000/users/1 完成302開始於FRI GET 「/用戶/ 1」 127.0.0.1 12月10處理由UsersController#展示作爲HTML 參數14時56分52秒-0800 2010 :{ (「users」。「id」= 1)限制1 CACHE(0.0ms)SELECT「users」用戶負載(3.1ms)SELECT「users」 。* FROM「users」WHERE(「users」。「id」= 1)限制1 渲染用戶/ _basic_info.html.erb(2.0ms) 渲染布局/_stylesheets.html.erb(5.2ms) 渲染布局/ _header.html.erb(12.1ms) 渲染布局/ _footer.html.erb(2.4ms) 佈局/應用程序中的渲染用戶/ show.html.erb(47.2ms) 在167ms內完成200 OK(查看:62.2ms | ActiveRecord的:6.6ms)

入門GET 「/avatars/small/missing.png」 爲127.0.0.1,在週五12月10日14時56分53秒-0800 2010

入門使用「/化身/小/失蹤巴紐」爲127.0.0.1,在週五12月10日14時56分53秒-0800 2010

的ActionController :: RoutingError(無路由匹配 「/avatars/small/missing.png」):

這裏是我的用戶控制器:

class UsersController < ApplicationController 
    def home 
    @user = User.find(params[:id]) 
    end 

    def index 
    @user = User.all 
    @title = "All users" 
    end 

    def show 
    @user = User.find(params[:id]) 
    @title = current_user.first_name 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     flash[:notice] = "Profile Updated" 
     redirect_to @user 
    else 
     render :action => 'new' 
    end 
    end 

    def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(params[:product]) 
     flash[:notice] = "Profile Updated" 
     redirect_to @user 
    else 
     render :action => 'edit' 
    end 
    end 


end 

這是我的用戶模型:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :lockable, :timeoutable and :activatable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :first_name, :last_name, :avatar 
    attr_accessible :bio, :sex, :birthday, :facebook, :twitter, :hometown, :current_city, :interested_in, :looking_for 

    validates :first_name, :presence => true, 
        :length => { :maximum => 50 } 

    validates :last_name, :presence => true, 
        :length => { :maximum => 50 } 

    has_attached_file :avatar, :styles => { :thumb => "50x50#", 
              :small => "150x150>", 
              :medium => "300x200>", 
              :large => "600x600>" }, 
        :url => "/assets/users/:id/:basename/:style.:extension", 
        :path => ":rails_root/public/assets/users/:id/:basename/:style.:extension" 


    validates_attachment_size :avatar, :less_than => 5.megabytes 
    validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] 


end 

我已經一遍又一遍地嘗試了很多東西,但不知道爲什麼文件不被保存。感謝您提前提供任何幫助。

+0

請編輯您的問題並設置您的代碼的格式。 – 2010-12-11 03:10:29

+0

你的表單中是否有multipart? – iain 2010-12-11 11:34:06

回答

2

你把標籤html => {:multipart => true}在你的表單中?

例子:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :multipart => true }) do |f| %> 
    <%= devise_error_messages! %> 
    <div class="blockForm"> 
    <p> 
     <%= f.label :name %> 
     <%= f.text_field :name %> 
    </p> 

    <p> 
     <%= f.label :email %> 
     <%= f.text_field :email %> 
    </p> 
    <p> 
     <%= f.label :avatar %> 
     <%= f.file_field :avatar %> 
    </p> 
    <p> 
     <%= f.label :password %> 
     <%= f.password_field :password %> 
     <span>Deixe em branco caso não queira alterar a senha</span> 
    </p> 

    <p> 
     <%= f.label :password_confirmation %> 
     <%= f.password_field :password_confirmation %> 
    </p> 

    <p> 
     <%= f.label :current_password %> 
     <%= f.password_field :current_password %> 
     <span>Coloque a sua senha para salvar as alterações</span> 
    </p> 
    </div> 
    <p><%= f.submit "Atualizar", :class => 'button' %></p> 
<% end %> 
0

不確定是否它的相關,但只注意到在你的代碼行

if @user.update_attributes(params[:product]) 

。是這個原因嗎?

相關問題