gpt4 book ai didi

javascript - Before_save不运行rails 4

转载 作者:行者123 更新时间:2023-12-03 12:17:41 24 4
gpt4 key购买 nike

将签名板的 JSON 转换为回形针图像轨

您好,有这个 Rails 4 应用程序,您可以签署表单并保存图片。我正在使用signaturePad.js,我正在关注tutorial将其转换为 img。我让 sig 表​​单正常工作并且它过去了数据,但使用 Paperclip 时出现错误

Paperclip::AdapterRegistry::NoHandlerError 

No handler found for "[{\"lx\":157,\"ly\":81,\"mx\":157,\"my\":80},{\"lx\":157,\"ly\":79,\"mx\":157,\"my\":81},{\"lx\":158,\"ly\":79,\"mx\":157,\"my\":79},{\"lx\":158,\"ly\":77,\"mx\":158,\"my\":79},{\"lx\":159,\"ly\":76,\"mx\":158,\"my\":77},{\"lx\":164,\"ly\":70,\"mx\":159,\"my\":76},{\"lx\":169,\"ly\":66,\"mx\":164,\"my\":70},{\"lx\":171,\"ly\":64,\"mx\":169,\"my\":66},{\"lx\":171,\"ly\":63,\"mx\":171,\"my\":64},{\"lx\":171,\"ly\":64,\"mx\":171,\"my\":63},

由于某种原因没有运行回调来制作 png。 我用谷歌搜索了几个月,没有任何帮助。谢谢到目前为止,这真是太棒了。

签名板.rb

    before_save :generate_image
has_attached_file :image # options ...

private

def signature_present
if signature.size <= 2
errors.add(:signature, :blank)
end
end

def generate_image
instructions = JSON.parse(signature).map { |h| "line #{h['mx'].to_i},#{h['my'].to_i} #{h['lx'].to_i},#{h['ly'].to_i}" } * ' '
image = StringIO.new
Open3.popen3("convert -size #{WIDTH}x#{HEIGHT} xc:transparent -stroke blue -draw @- PNG:-") do |input, output, error|
input.puts instructions
input.close
image.puts(output.read)
end
self.image = image
end
end

我的看法

<%= form_tag({:controller => "sigPad", :action => "signature", }, :method => "post", :class => "sigPad") do %>
<a href="#draw-it">Firma</a>
<a href="#clear">Borrar</a>
<div class="sig sigWrapper">
<div class="typed"></div>
<canvas class="pad" width="390" height="110"></canvas>
<input id:"signature" type="hidden" name="signature" class="output" value="output">
</div>
<%= submit_tag("Guardar") %>
<%end%>

JS

      $(function() {
$('.sigPad')
.signaturePad({
drawOnly: true,
validateFields: false,
lineTop:80
})

});

最佳答案

Paperclip 可能会在调用 before_save 之前尝试处理它。也许能够让它与 before_validate 一起工作。否则,您可以将该代码重构为一个单独的类,并在将参数从请求传递到模型时在 Controller 中执行此操作。

关于javascript - Before_save不运行rails 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24602815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com