CarrierWave remote_image_url 未保存

CarrierWave remote_image_url not saving

我有一个 Rails 4 应用程序,我正在使用 CarrierWave 从 url 抓取图像。在我的 form_for 中,url 很好地传递到参数中,但我似乎无法保存 url。当我查看最后保存的 Stamp 时,remote_image_urlnil。我敢肯定这很简单,但是文档非常糟糕。

并确认一下; CarrierWave 在使用表单中的 f.file_field 从文件上传图像时完美运行。

这是我的代码:

stamp_uploader.rb:

class Stamp < ActiveRecord::Base
  mount_uploader :image, StampUploader
  mount_uploader :remote_image_url, StampUploader
end

stamps_controller:

def show
    @stamp = Stamp.find(params[:id])
end

def new
    @stamp = Stamp.new
end

def create
    @stamp = Stamp.create(stamp_params)

    if @stamp.save
        flash[:success] = "Thanks for your submission!"
        redirect_to root_path
    else
        render :new
    end
end

private

def stamp_params
    params.require(:stamp).permit(:image, :remote_image_url)
end

new.html.erb:

<%= form_for @stamp do |f| %>
  <%= image_tag(current_user.image) %>
  <%= f.label :remote_image_url, "Upload Image" %>
  <%= f.text_field :remote_image_url, value: current_user.image %>

  <%= f.submit %>
<% end %>

schema.rb:

create_table "stamps", force: :cascade do |t|
  t.string   "image"
  t.datetime "created_at",        null: false
  t.datetime "updated_at",        null: false
  t.string   "remote_avatar_url"
  t.string   "remote_image_url"
end

参数:

=> {"utf8"=>"✓",
 "authenticity_token"=>"rA8KaI+bG5ygldeQC2n00z3BuEfiA0xO4tukUUL3tvG19G7WQCuMMqwzwWzWSMbKlT+2W5KsJYF4Q/lXg9OHeA==",
 "stamp"=>{"remote_image_url"=>"https://graph.facebook.com/10157057736060574/picture?width=1000&height=1000"},
 "commit"=>"Create Stamp",
 "controller"=>"stamps",
 "action"=>"create"}

我认为你的台词 mount_uploader :remote_image_url, StampUploader 搞砸了。尝试删除它,看看是否有效。

当您执行 mount_uploader :foo, StampUploader 时,我相信 CarrierWave 会为您定义(除其他外)foo=remote_foo_url= 方法。

因此,当您稍后执行 mount_uploader :remote_image_url, StampUploader 时,您将覆盖刚刚使用另一种方法获得的 remote_image_url= 方法,尽管它的名称需要一个文件,而不是 URL。

如果这有帮助,我是否可以建议您通过向 CarrierWave 发出拉取请求以澄清文档(如果您发现它们不清楚)来转发它? :)