[英]Paperclip url is not working with image_tag
I know this sounds like a stupid question, but I really don't see the root cause right now... 我知道这听起来像是一个愚蠢的问题,但我现在真的看不到根本原因...
I just figured out to upload image using ajax with the help of jQuery-File-Upload and Paperclip, everything is quite fine, the file is uploaded, the database entry is successfully saved. 我只是想借助jQuery-File-Upload和Paperclip使用ajax上传图像,一切都很好,文件已上传,数据库条目已成功保存。
The only problem is the <%= image_tag @attached_image.image.url %>
is not working in my create.js.erb, with which I would like to make an entry on the view with javascript. 唯一的问题是
<%= image_tag @attached_image.image.url %>
在我的create.js.erb中不起作用,我想使用javascript在视图上进行输入。 But the url of the uploaded file instance is correct. 但是上传的文件实例的网址正确。
it's working if I do: 如果我这样做的话,它是可行的 :
$("#attached-images").append("<li><img src='http://localhost:3000<%= @attached_image.image.url %>'></img></li>")
but it's not working if I do, which is from Paperclips Github Readme: 但是如果我这样做是行不通的 ,那是来自Paperclips Github自述文件:
$("#attached-images").append("<li><%= image_tag @attached_image.image.url %></li>")
and there's no exception raised in the console, there's just nothing comes up. 控制台中也没有异常,什么也没发生。 I am really confused..and is there anyway that I could debug the js.erb processing in the browser?
我真的很困惑。无论如何,我可以在浏览器中调试js.erb处理吗?
My view: 我的观点:
<div id="upload-and-insert-image-dialog">
<%= form_for :attached_image, :remote => true, :url => attached_images_path(), :html => { :class => "upload", :multipart => true } do |f| %>
<%= f.file_field :image %>
<% end %>
</div>
<ul id="attached-images">
</ul>
My controller: 我的控制器:
class AttachedImagesController < ApplicationController
def create
@attached_image = AttachedImage.new(params[:attached_image])
respond_to do |format|
if @attached_image.save
format.js
else
format.js { render 'attached_images/create_error' }
end
end
end
end
My attached_images/create.js.erb: 我的attach_images / create.js.erb:
$("#attached-images").append("<li><%= image_tag @attached_image.image.url %></li>")
My model: 我的模特:
class AttachedImage < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
has_attached_file :image
end
I found the problem, it's because that I missed the escape_javascript
function, the following code works very well. 我发现了问题,这是因为我错过了
escape_javascript
函数,下面的代码运行得很好。
$("#attached-images").append("<li><%= escape_javascript(image_tag(@attached_image.image.url)) %></li>")
many thanks to @varatis, you remind me of that. 非常感谢@varatis,您让我想起了这一点。
Maybe something along the lines of this would work: 也许与此类似的东西会起作用:
$('#attend-images').append('<%= escape_javascript(render partial: 'images') %>');
where _images.html.erb is partial under the same folder that looks like: 其中_images.html.erb是位于同一文件夹下的部分文件夹,如下所示:
<li><%= image_tag @attached_image.image.url %></li>
I'm unsure if this exact syntax would work though. 我不确定这种确切的语法是否会起作用。 I've done things similar but worst case scenario, you make want to just put all of your attached images in a partial, and refresh it once you get the request like so (in your create.js.erb):
我做过类似的事情,但最坏的情况是,您只想将所有附加的图像放到一部分中,并在收到请求后刷新它(在create.js.erb中):
$('#attend-images').html('<%= escape_javascript(render partial: 'images') %>');
and then _images.html.erb is just a partial which contains your ul of images (since that's what I'm assuming you want to append to): 然后_images.html.erb只是其中的一部分,其中包含您的所有图片(因为这是我假设要附加的内容):
<% for image in @images %>
<ul>
....
<li><%= image_tag image.image.url %></li>
....
</ul>
<% end %>
This is because inside @attached_image.image.url
host is missing hence use the image_path helper which is image_tag uses to generate the url for html img tag. 这是因为缺少
@attached_image.image.url
主机,因此使用image_tag用于生成html img标签的url的image_path帮助器。 It wont be problem for s3 storage. s3存储不会有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.