我在我的应用程序中实现了一个图片上传功能。图片模型是多态的,它既属于用户,也属于记分板。下面给出了图像控制器。
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>图片/上载代码在记分板显示页上呈现新窗体的部分。我正在使用载波和极小集上传这张照片。这方面的代码如下所示。
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'我在载波中发现了这一点,这可能与这个问题有关。
"/images/fallback/" + [version_name, "4.png"].compact.join('_')发布于 2015-12-13 07:15:28
这里的主要问题是在控制器中使用实例vars:
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请注意,如果您使用的是多态路由助手,这将将表单方法更改为修补程序,如下所示:
<%= form_for(@picture) do |f| %>
<%# ... %>
<% end %>这是因为@picture被持久化了。如果要修改窗体以始终发布新图片,则可以使用:
<%= form_for(@pictureable.build_picture) do |f| %>
<%# ... %>
<% end %>https://stackoverflow.com/questions/34247522
复制相似问题