新手问题 CarrierWave 文件上传问题:

hujoy · 2012年05月22日 · 最后由 hujoy 回复于 2012年05月22日 · 5320 次阅读

CarrierWave 和 Upyun 都能正常使用,已经验证过。但:cover 无法上传,以下是源代码:

Model 中 topic.rb:

mount_uploader :cover, TopicCoverUploader

Uploader 中 topic_cover_uploader.rb:

# coding: utf-8
class TopicCoverUploader < BaseUploader
  # To be shown in group, such as topics list page
  version :normal do
    process :resize_to_fill => [240, 180]
  end

  version :medium do
    process :resize_to_fill => [320, 240]
  end

  # To be shown individually, such as topic page  
  version :large do
    process :resize_to_fill => [640, 480]
  end
end

视图:views/topics/_form.heml.erb

<%= simple_form_for @topic, :html => {:multipart => true} do |f| %>
<%= render "shared/error_messages", :target => @topic %> 
  <fieldset>
    <legend></legend>
    <%= f.input :title %>
    <%= image_tag(@topic.cover_url) if @topic.cover? %>
    <%= f.file_field :cover %>
    <%= f.hidden_field :cover_cache %>
    <div class="form-actions">
      <%= f.submit t("common.save"), :class => "btn btn-primary", :disable_with => t("common.saving") %>
      <%= link_to(t("common.cancel"), @topic.id.blank? ? root_path : topic_path(@topic), :class => "btn")%>
    </div>
  </fieldset>
<% end %>

但一直无法上传,坛友们帮忙看一下是什么问题?

匿名 #2 2012年05月22日
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end


匿名 #3 2012年05月22日

让我看看你的 shared/error_messages

BTW: 你应该把 log 贴出来

error_messages 无任何错误提示,development.log 也已经查过,没有错误提示。整个过程无任何错误提示,只是文件不会上传,然后:cover 为 nil

Started GET "/topics/new" for 127.0.0.1 at 2012-05-22 21:01:14 +0800
Processing by TopicsController#new as HTML
[36mMONGODB[0m (0ms) hujoy_db_development_apr5['[34musers[0m'].[33mfind[0m({:_id=>14}).limit(-1).sort([[:_id, :asc]])
[35mMONGODB[0m (0ms) hujoy_db_development_apr5['[34m$cmd[0m'].[33mfind[0m({"count"=>"notifications", "query"=>{"user_id"=>14, :read=>false}, "fields"=>nil}).limit(-1)
  Rendered topics/_base.html.erb (1.9ms)
  Rendered shared/_error_messages.html.erb (0.0ms)
[36mMONGODB[0m (1ms) hujoy_db_development_apr5['[34msections[0m'].[33mfind[0m({}).sort([[:sort, :asc]])
[35mMONGODB[0m (1ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({"section_id"=>1}).sort([[:sort, :asc]])
[36mMONGODB[0m (0ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({"section_id"=>2}).sort([[:sort, :asc]])
[35mMONGODB[0m (0ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({"section_id"=>3}).sort([[:sort, :asc]])
[36mMONGODB[0m (1ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({"section_id"=>4}).sort([[:sort, :asc]])
[35mMONGODB[0m (1ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({"section_id"=>5}).sort([[:sort, :asc]])
[36mMONGODB[0m (1ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({"section_id"=>6}).sort([[:sort, :asc]])
[35mMONGODB[0m (1ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({"section_id"=>7}).sort([[:sort, :asc]])
[36mMONGODB[0m (1ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({"section_id"=>8}).sort([[:sort, :asc]])
[35mMONGODB[0m (1ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({"section_id"=>9}).sort([[:sort, :asc]])
  Rendered topics/_editor_toolbar.html.erb (1.0ms)
  Rendered topics/_form.html.erb (61.1ms)
  Rendered app/cells/topics/sidebar_for_new_topic_node.html.erb (0.0ms)
  Rendered app/cells/topics/reply_help_block.zh-CN.html.erb (0.0ms)
  Rendered topics/_sidebar_for_new_topic.html.erb (5.1ms)
  Rendered topics/new.html.erb within layouts/application (69.5ms)
  Rendered common/_search_form.html.erb (0.2ms)
  Rendered common/_user_nav.html.erb (1.8ms)
  Rendered common/_google_analytics.html.erb (0.1ms)
Completed 200 OK in 139ms (Views: 118.3ms | Mongo: 11.7ms | Solr: 0.0ms)


Started POST "/topics" for 127.0.0.1 at 2012-05-22 21:01:37 +0800
Processing by TopicsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "topic"=>{"node_id"=>"7", "title"=>"显式显示type是否会成功。", "cover"=>#<ActionDispatch::Http::UploadedFile:0x000000083ec530 @original_filename="facebook.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"topic[cover]\"; filename=\"facebook.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20120522-2435-qsoh6v>>, "cover_cache"=>"", "body"=>"```\r\n    <%= image_tag(@user.avatar_url) if @user.avatar? %>\r\n    <%= f.file_field :avatar %>\r\n    <%= f.hidden_field :avatar_cache %>\r\n```"}, "commit"=>"保存"}
[36mMONGODB[0m (0ms) hujoy_db_development_apr5['[34musers[0m'].[33mfind[0m({:_id=>14}).limit(-1).sort([[:_id, :asc]])
[35mMONGODB[0m (0ms) hujoy_db_development_apr5['[34m$cmd[0m'].[33mfind[0m({"count"=>"notifications", "query"=>{"user_id"=>14, :read=>false}, "fields"=>nil}).limit(-1)
WARNING: Can't mass-assign protected attributes: node_id, cover, cover_cache
WARNING: Can't mass-assign protected attributes: node_id, cover, cover_cache
[36mMONGODB[0m (0ms) hujoy_db_development_apr5['[34musers[0m'].[33mfind[0m({:login=>/^(user|user)$/i, :_id=>{"$ne"=>14}}, {:_type=>1, :_id=>1}).limit(5)
[35mMONGODB[0m (0ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mfind[0m({:_id=>7}).limit(-1).sort([[:_id, :asc]])
[36mMONGODB[0m (0ms) hujoy_db_development_apr5['[34m$cmd[0m'].[33mfind[0m({:findandmodify=>"mongoid.auto_increment_ids", :query=>{:_id=>"topics"}, :update=>{:$inc=>{:c=>1}}, :new=>true, :upsert=>true}).limit(-1)
[35mMONGODB[0m (0ms) hujoy_db_development_apr5['[34mtopics[0m'].[33minsert[0m([{"mentioned_user_ids"=>[], "replies_count"=>0, "follower_ids"=>[], "likes_count"=>0, "tags"=>[], "title"=>"显式显示type是否会成功。234", "body"=>"```\r\n    <%= image_tag(@user.avatar_url) if @user.avatar? %>\r\n    <%= f.file_field :avatar %>\r\n    <%= f.hidden_field :avatar_cache %>\r\n\n```", "_type"=>"Topic", "user_id"=>14, "node_id"=>7, "updated_at"=>2012-05-22 13:01:37 UTC, "body_html"=>"<div class=\"highlight\"><pre>    &lt;%= image_tag(<a href=\"/users/user\" class=\"at_user\" title=\"@user\"><i>@</i>user</a>.avatar_url) if <a href=\"/users/user\" class=\"at_user\" title=\"@user\"><i>@</i>user</a>.avatar? %&gt;\n    &lt;%= f.file_field :avatar %&gt;\n    &lt;%= f.hidden_field :avatar_cache %&gt;\n</pre>\n</div>", "node_name"=>"杂志", "node_slug"=>"journals", "created_at"=>2012-05-22 13:01:37 UTC, "replied_at"=>2012-05-22 13:01:37 UTC, "_id"=>28}])
[36mMONGODB[0m (0ms) hujoy_db_development_apr5['[34mnodes[0m'].[33mupdate[0m({"_id"=>7}, {"$inc"=>{"topics_count"=>1}})
[35mMONGODB[0m (0ms) hujoy_db_development_apr5['[34musers[0m'].[33mupdate[0m({"_id"=>14}, {"$inc"=>{"topics_count"=>1}})
[36mMONGODB[0m (0ms) hujoy_db_development_apr5['[34musers[0m'].[33mfind[0m({:_id=>{"$in"=>[]}})
  [1m[32mSOLR Request (10.0ms)[0m  [ path=#<RSolr::Client:0x00000008c3a9a8> parameters={data: [1m[1m<?xml version="1.0" encoding="UTF-8"?><add><doc><field name="id">Topic 28</field><field name="type">Topic</field><field name="class_name">Topic</field><field name="node_id_i">7</field><field name="replied_at_d">2012-05-22T13:01:37Z</field><field name="title_textsv">显式显示type是否会成功。234</field><field name="body_textsv">```
    &lt;%= image_tag(@user.avatar_url) if @user.avatar? %&gt;
    &lt;%= f.file_field :avatar %&gt;
    &lt;%= f.hidden_field :avatar_cache %&gt;

```</field></doc></add>[0m, headers: [1m[1m{"Content-Type"=>"text/xml"}[0m, method: [1m[1mpost[0m, params: [1m[1m{:wt=>:ruby}[0m, query: [1m[1mwt=ruby[0m, path: [1m[1mupdate[0m, uri: [1m[1mhttp://localhost:8983/solr/update?wt=ruby[0m, open_timeout: [1m[1m[0m, read_timeout: [1m[1m[0m} ]
Redirected to http://hujoy.com:3000/topics/28
  [1m[32mSOLR Request (533.6ms)[0m  [ path=#<RSolr::Client:0x00000008c3a9a8> parameters={data: [1m[1m<?xml version="1.0" encoding="UTF-8"?><commit/>[0m, headers: [1m[1m{"Content-Type"=>"text/xml"}[0m, method: [1m[1mpost[0m, params: [1m[1m{:wt=>:ruby}[0m, query: [1m[1mwt=ruby[0m, path: [1m[1mupdate[0m, uri: [1m[1mhttp://localhost:8983/solr/update?wt=ruby[0m, open_timeout: [1m[1m[0m, read_timeout: [1m[1m[0m} ]
Completed 302 Found in 1345ms


Started GET "/topics/28" for 127.0.0.1 at 2012-05-22 21:01:38 +0800

以下是显示新创建的topic

主要可以看这一段:

Processing by TopicsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "topic"=>{"node_id"=>"7", "title"=>"显式显示type是否会成功。", "cover"=>#<ActionDispatch::Http::UploadedFile:0x000000083ec530 @original_filename="facebook.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"topic[cover]\"; filename=\"facebook.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20120522-2435-qsoh6v>>, "cover_cache"=>"", "body"=>"```\r\n    <%= image_tag(@user.avatar_url) if @user.avatar? %>\r\n    <%= f.file_field :avatar %>\r\n    <%= f.hidden_field :avatar_cache %>\r\n```"}, "commit"=>"保存"}
匿名 #7 2012年05月22日

我的意思是我想参考一下你的 error_message 的写法,我的 rails3.2.3 的 error_message 从来没有显示过错误信息

匿名 #8 2012年05月22日
WARNING: Can't mass-assign protected attributes: node_id, cover, cover_cache

把你 topic 的 model 拿出来看看

#7 楼 @sharp 呵呵,ruby-china 的源程序中有的,/app/views/shared/_error_messages.html.erb 中。

贴一段能正常上传的:

Processing by AccountController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "user"=>{"name"=>"hujoy", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x0000000884a1e0 @original_filename="Revolutionary.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"Revolutionary.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20120403-1681-aw1qkz>>, "email"=>"[email protected]", "location"=>"", "github"=>"", "website"=>"", "tagline"=>"", "bio"=>""}, "commit"=>"更新资料"}

#10 楼 @hujoy 貌似没有区别。

#8 楼 @sharp 找到问题所在啦,没有 attr_accessible。你看得真仔细呀,谢谢!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号