CarrierWave remote_image_url不保存

时间:2016-06-17 14:26:39

标签: ruby-on-rails ruby ruby-on-rails-4 carrierwave

我有一个Rails 4应用程序,我正在使用CarrierWave从网址抓取图像。在我的form_for url被传递到params就好了,但我似乎无法得到保存的URL。当我查看最后Stamp次保存时,remote_image_urlnil。我确信这很简单,但文档非常糟糕。

只是为了确认;使用表单中的f.file_field从文件上传图像时,CarrierWave可以正常工作。

这是我的代码:

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

PARAMS:

=> {"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"}

1 个答案:

答案 0 :(得分:0)

我认为你的专栏mount_uploader :remote_image_url, StampUploader会让人感到困惑。尝试删除它,看看是否有效。

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

因此,当您稍后执行mount_uploader :remote_image_url, StampUploader时,您将覆盖使用其他方法获得的remote_image_url=方法,尽管其名称需要文件,而不是网址。

如果这有帮助,如果你发现它们不清楚,我可以建议你通过向CarrierWave提出拉动请求来澄清文档吗? :)