【发布时间】:2019-06-14 13:07:58
【问题描述】:
我有一个表格来添加图片网址form, 如果用户添加图片 url 我想下载图片并保存到本地磁盘(home/documents/image)
我创建图像模型和控制器..
需要帮助
在图像模型中
class Image < ApplicationRecord
has_one_attached :image
require 'open-uri'
image = open('http://example.com/image.png')
IO.copy_stream(image, '~/home/Documents/Images/local_image.png')
end
在图像控制器中
class ImagesController < ApplicationController
def index
@image = Image.new
end
end
在图片/index.html.erb中
<%= form_for(@image, url: images_path) do |f| %>
<%= f.text_field :image_url %>
<%=f.submit%>
<% end %>
但出现错误
OpenURI::HTTPError in ImagesController#index 404 not found
【问题讨论】:
-
请为
image模型和控制器添加代码并查看(表单)。 -
这个问题似乎根本不包括解决问题的任何尝试。StackOverflow 希望您try to solve your own problem first,因为您的尝试有助于我们更好地了解您想要什么。请编辑问题以显示您尝试过的内容,并使用Minimal, Complete, and Verifiable 示例显示您遇到的特定障碍。欲了解更多信息,请参阅How to Ask
-
在表单提交操作中获取插入的 URL,然后应用逻辑下载它。
-
标签: ruby-on-rails ruby