Ruby 1.87 Rails 3.03和Devise。回形针图像虽然显示在日志文件中但未保存

时间:2010-12-11 01:03:33

标签: ruby-on-rails paperclip devise

我已经得到了回形针,可以毫无问题地在我的系统上工作。在我最近构建的应用程序中,出于某种原因,我无法使用paperclip来保存图像,即使日志文件显示它也不会显示在sql数据中。

我唯一的区别就是使用Devise并想知道是否需要更改一些额外的设置。

日志文件说:

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

UsersController处理#update为HTML   参数:{“commit”=>“Update”,“authenticity_token”=>“JexxammsT + VPzqwbLr / YohuY4vcpBFdfVrOwDjDQEUo =”,“utf8”=>“✓”,“id”=>“1”,“用户“=> {”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“>}}   用户负载(3.1ms)SELECT“users”。* FROM“users”WHERE(“users”。“id”= 1)LIMIT 1   用户负载(0.5ms)SELECT“users”。“id”FROM“users”WHERE(“users”。“email”='mrussellharrison@gmail.com')AND(“users”.id<> 1)LIMIT 1 [回形针]保存附件。 重定向到http://localhost:3000/users/1 完成302发现在124ms

于2010年12月10日星期五14:56:52 -0800开始获取127.0.0.1的“/ users / 1”   由UsersController处理#show as HTML   参数:{“id”=>“1”}   用户负载(3.1ms)SELECT“users”。* FROM“users”WHERE(“users”。“id”= 1)LIMIT 1   CACHE(0.0ms)SELECT“users”。* FROM“users”WHERE(“users”。“id”= 1)LIMIT 1 呈现的用户/ _basic_info.html.erb(2.0ms) 渲染布局/ _stylesheets.html.erb(5.2ms) 渲染布局/ _header.html.erb(12.1ms) 渲染布局/ _footer.html.erb(2.4ms) 在布局/应用程序中呈现users / show.html.erb(47.2ms) 在167ms完成200 OK(浏览次数:62.2ms | ActiveRecord:6.6ms)

于2010年12月10日星期五14:56:53 -0800开始获取127.0.0.1的“/avatars/small/missing.png”

于2010年12月10日星期五14:56:53 -0800开始获取127.0.0.1的“/avatars/small/missing.png”

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

我一遍又一遍地尝试了很多东西,但不确定为什么文件没有被保存。感谢您提前提供任何帮助。

2 个答案:

答案 0 :(得分:2)

你把标签 html =&gt; {:multipart =&gt;您的表单中是真的吗?

示例:

<%= 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 %>

答案 1 :(得分:0)

不确定它是否相关,但只是注意到了

if @user.update_attributes(params[:product])
你的代码中的

。那是什么原因?