通过回形针保存URL中的图片 - 未经许可的参数

时间:2013-12-12 15:24:06

标签: ruby-on-rails paperclip

我在rails(4.0.1)上使用ruby(2.0.0),我尝试使用paperclip从URL上传图片。我已经按照本教程但没有保存任何内容。 http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

迁移以添加照片远程网址

class AddImageRemoteUrlToPhotos < ActiveRecord::Migration

  def self.up
    add_column :photos, :image_remote_url, :string
  end

  def self.down
    remove_column :photos, :image_remote_url
  end
end

schema.rb

create_table "photos", force: true do |t|
    t.integer  "user_id"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.string   "image_file_size"
    t.string   "image_update_at"
    t.string   "content"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image_remote_url"
  end

photo.rb

require "open-uri"

class Photo < ActiveRecord::Base
    belongs_to :user
    attr_accessor :image_url
    has_attached_file :image,
                :styles => { :medium => "x300", :thumb => "x100" },
                :default_url => "www.google.fr",
                :storage => :s3,
                :s3_host_name => 's3-eu-west-1.amazonaws.com',
                :bucket => 'pyloriruc',
                :s3_credentials => S3_CREDENTIALS

before_validation :download_remote_image, :if => :image_url_provided?
validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'

private

  def image_url_provided?
    !self.image_url.blank?
  end

  def download_remote_image
    self.image = do_download_remote_image
    self.image_remote_url = image_url
  end

  def do_download_remote_image
    io = open(URI.parse(image_url))
    def io.original_filename; base_uri.path.split('/').last; end
    io.original_filename.blank? ? nil : io
  rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
  end

end

照片控制器

 def create
    @photo = Photo.new(photo_params)

    respond_to do |format|
      if @photo.save
        format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
        format.json { render action: 'show', status: :created, location: @photo }
      else
        format.html { redirect_to 'new'}
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end 

查看

<%= form_for @photo, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
     Upload a photo: <%= f.file_field :image %>

  ...or provide a URL: <%= f.text_field :image_url %>
</div>
<%= f.submit "Upload picture", class: "btn btn-large btn-primary" %>
<% end %>

当我提交一个URL时,我可以看到一切都通过了,但在我的数据库中没有保存任何东西。这是我的日志:

Started PATCH "/photos/126" for 127.0.0.1 at 2013-12-12 16:14:50 +0100
Processing by PhotosController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ht1dYUaRyZcec8DQE3QgU6YSfCkEwbD4TfeBwL9iQS4=", "photo"=>{"image_url"=>"http://cdn.pratique.fr/sites/default/files/articles/lapin-blanc.jpg"}, "commit"=>"Upload picture", "id"=>"126"}
  [1m[35mPhoto Load (0.2ms)[0m  SELECT "photos".* FROM "photos" WHERE "photos"."id" = ? LIMIT 1  [["id", "126"]]
Unpermitted parameters: image_url
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35m (0.1ms)[0m  commit transaction
Redirected to http://localhost:3000/photos/126
Completed 302 Found in 4ms (ActiveRecord: 0.3ms)


Started GET "/photos/126" for 127.0.0.1 at 2013-12-12 16:14:50 +0100
Processing by PhotosController#show as HTML
  Parameters: {"id"=>"126"}
  [1m[36mPhoto Load (0.1ms)[0m  [1mSELECT "photos".* FROM "photos" WHERE "photos"."id" = ? LIMIT 1[0m  [["id", "126"]]
  Rendered shared/_error_messages.html.erb (0.0ms)
  Rendered photos/show.html.erb within layouts/application (2.7ms)
  Rendered layouts/_shim.html.erb (0.2ms)
  [1m[35mUser Load (0.2ms)[0m  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '6560a87f84c7b093f515e90ba69671d68b8fde4b' LIMIT 1
  Rendered layouts/_header.html.erb (2.1ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 17ms (Views: 15.6ms | ActiveRecord: 0.3ms)

我不明白为什么会得到: 未经许可的参数:image_url

当我在控制台的教程中尝试时:

2.0.0p247 :001 > Photo.new(:image_url => 'http://www.google.com/intl/en_ALL/images/logo.gif')
 => #<Photo id: nil, user_id: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_update_at: nil, content: nil, created_at: nil, updated_at: nil, image_remote_url: nil>

1 个答案:

答案 0 :(得分:0)

我不确定你是否想过这个,但我遇到了同样的问题。

解决方案(至少对我而言)在params中 - 尝试将:image_url添加到允许的参数列表中 - 我有类似的东西

def photo_params
  params.require(:photo).permit(:description, :image, :image_url)
end