[英]Save a picture from URL via paperclip - unpermitted parameters
我在rails(4.0.1)上使用ruby(2.0.0),並且嘗試使用回形針從URL上傳圖片。 我已按照本教程進行操作,但未保存任何內容。 http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/
遷移以添加照片遠程URL
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>
我不確定您是否能解決這個問題,但是我遇到了同樣的問題。
解決方案(至少對我而言)在參數中–嘗試將:image_url
添加到允許的參數列表中–我有類似的東西
def photo_params
params.require(:photo).permit(:description, :image, :image_url)
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.