我在我的应用中实现了图片上传功能。图片模型是多态的,它既属于用户又属于记分板。图片控制器如下。
class PicturesController < ApplicationController
before_filter :load_pictureable
def create
@picture = @pictureable.build_picture(picture_params)
if @picture.save
flash[:success] = "Uploaded Successfully"
redirect_to @pictureable
else
render 'scoreboards/show'
end
end
def picture_params
params.require(:picture).permit(:picture)
end
def load_pictureable
resource, id = request.path.split('/')[1,2]
@pictureable = resource.singularize.classify.constantize.find(id)
end
end
图片上传到scoreboard show
页面。视图和控制器中的相关代码如下所示。
before_filter :load_pictureable
def show
@scoreboard = Scoreboard.find_by_id(params[:id])
@team = @scoreboard.teams.build
@comment = @scoreboard.comments.new
@schedule = @scoreboard.schedules.build
@picture = @pictureable.build_picture
end
def load_pictureable
resource, id = request.path.split('/')[1,2]
@pictureable = resource.singularize.classify.constantize.find(id)
end
观看代码
<div>
<%= render 'pictures/upload' %>
<%= image_tag @picture.picture_url if @picture.picture_url.present? %>
</div>
图片/上传代码在记分板显示页面上呈现新表单的部分内容。我正在使用carrierwave和minimagick上传这张照片。代码如下所示。
class Picture < ActiveRecord::Base
belongs_to :pictureable, polymorphic: true
mount_uploader :picture, PictureUploader
end
class PictureUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
process resize_to_limit: [200, 171]
end
我遇到的问题是我无法查看图片。模型,关联,创建动作和其他一切都非常好。我检查了日志文件并创建了它。问题是查看图像。代码<%= image_tag @picture.picture_url if @picture.picture_url.present? %>
不显示图像。它显示了一个带有数字4的小图像标签,我不知道为什么。我不确定它的载波是否会导致问题或其他问题。我现在已经查看了几次代码,但无法调试问题。我很确定它的东西很小。我试图保持代码简洁明了。如果需要任何其他代码,请告诉我们。一如既往,任何帮助将不胜感激。谢谢!
日志文件显示错误。代码如下。
创建操作的日志文件中的代码。
Started POST "/scoreboards/13/pictures" for 174.93.38.248 at 2015-12-13 03:29:08 +0000
Processing by PicturesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6GAaLHM6gRRgL6Fi7YdlkHbgIqUG4BXHzBL4cNuu1EOhSpHfcUqPKkwHJ2qLV4sD5ZFNGJaaRMpMCnZE31BL3w==", "picture"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0x007f70773b8e38 @tempfile=#<Tempfile:/home/ubuntu/workspace/RackMultipart20151213-14799-hppbvb.jpg>, @original_filename="10464404_515284598620752_6141163632807961194_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"picture[picture]\"; filename=\"10464404_515284598620752_6141163632807961194_n.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"upload photo", "scoreboard_id"=>"13"}
[1m[36mScoreboard Load (10.3ms)[0m [1mSELECT "scoreboards".* FROM "scoreboards" WHERE "scoreboards"."id" = ? ORDER BY "scoreboards"."created_at" DESC LIMIT 1[0m [["id", 13]]
[1m[35mPicture Load (0.3ms)[0m SELECT "pictures".* FROM "pictures" WHERE "pictures"."pictureable_id" = ? AND "pictures"."pictureable_type" = ? LIMIT 1 [["pictureable_id", 13], ["pictureable_type", "Scoreboard"]]
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
[1m[35mSQL (0.6ms)[0m INSERT INTO "pictures" ("picture", "pictureable_id", "pictureable_type", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["picture", "10464404_515284598620752_6141163632807961194_n.jpg"], ["pictureable_id", 13], ["pictureable_type", "Scoreboard"], ["created_at", "2015-12-13 03:29:08.787873"], ["updated_at", "2015-12-13 03:29:08.787873"]]
[1m[36m (11.3ms)[0m [1mcommit transaction[0m
Redirected to https://score-app-kpauls.c9.io/scoreboards/13
Completed 302 Found in 262ms (ActiveRecord: 22.7ms)
Started GET "/images/fallback/4.png" for 174.93.38.248 at 2015-12-13 03:19:12 +0000
ActionController::RoutingError (No route matches [GET] "/images/fallback/4.png"):
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
我在carrierwave中发现这可能与此问题有关。
"/images/fallback/" + [version_name, "4.png"].compact.join('_')
答案 0 :(得分:1)
这里的主要问题是您在控制器中使用实例变量:
def show
@scoreboard = Scoreboard.find_by_id(params[:id])
@team = @scoreboard.teams.build
@comment = @scoreboard.comments.new
@schedule = @scoreboard.schedules.build
@picture = @pictureable.build_picture
end
在这里,您要将@picture
分配给适用于表单的新图片,但不能显示属于@pictureable
的图片。
由于@picture
始终是新图片,因此CarrierWave将返回后备路径,因为您无法路由到尚未保留的记录。
那你怎么解决这个问题?
@picture = @pictureable.picture || @pictureable.build_picture
请注意,如果您使用多态路由助手,这会将表单方法更改为PATCH:
<%= form_for(@picture) do |f| %>
<%# ... %>
<% end %>
这是因为@picture
已被保留。如果要修改表单以始终POST新图片,请改为使用:
<%= form_for(@pictureable.build_picture) do |f| %>
<%# ... %>
<% end %>