私は、レールアプリに取り組んでいて、画像のネストされたリソースを作っています。モデルはPicture
であり、多型であり、いくつかの異なるテーブルと対話します。rails paperclip:paramが見つからないか、値が空です。
私はコントローラアクションとcreateメソッドを処理しています。 this answerによると、私はペーパークリップで見たことがありますが、パラメータはtable_name
の形式に従ってください。次にfile
に従ってください。それを行っても、私はまだparam is missing or the value is empty: picture
エラーが発生しています。ここで
は私のコードです:
Picture.rb
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_attached_file :image, style: { small: '64x64', medium: '100x100', large: '200x200' }
validates_attachment :image, presence: true, content_type: { content_type: /\Aimage\/.*\Z/ },
size: { in: 0..5.megabytes }, default_url: 'missing_img.png'
acts_as_list scope: [:imageable_id, :imageable_type]
end
pictures_controller.rb
class PicturesController < ApplicationController
before_action :authorize_user!
before_action :set_resource!
def index
@pictures = @resource.pictures
end
def create
@picture = @resource.pictures.new(picture_params) do |pic|
pic.imageable_type = @resource
pic.imageable_id = @resource.id
end
if @picture.save
redirect_to :back
flash[:success] = 'Image Saved!'
else
redirect_to :back
flash[:danger] = "#{@picture.errors.full_messages.to_sentence}"
end
end
def destroy
@picture = Picture.find(params[:id])
@resource.pictures.delete(@picture)
redirect_to :back
flash[:success] = "Picture deleted"
end
private
def set_resource!
klass = [Gym, User, Location, Product].detect { |c| params["#{c.name.underscore}_id"] }
@resource = klass.find(params["#{klass.name.underscore}_id"])
end
def picture_params
params.require(:picture).permit(:image)
end
end
写真/ index.htmlを
<h6>Upload Pictures</h6>
<%= form_for(current_user, url: url_for(controller: 'pictures', action: 'create'), method: :post, html: { multipart: true, class: 'form-horizontal' }) do |f| %>
<%= f.file_field :image, type: :file, multiple: true, style: 'padding-bottom: 25px;' %>
<%= f.submit "Upload", class: "btn btn-gen" %>
<% end %>
そして、ここでは、フォーム上のparam要求が提出されています。でも、これで
{"utf8"=>"✓",
"authenticity_token"=>"+fsA6liECF7pkUp/0BA0wDHq9Vv63jB+WBb7O/uUEDhhmIOZ22Rb1rNWDwuwPTDPNS7jg7vP/fVCVllDV21wDw==",
"user"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x007f87b138acf8 @tempfile=#<Tempfile:/var/folders/9c/1_0mk00n297f560_fpv9jzl40000gn/T/RackMultipart20161020-25582-3qt3gc.jpg>,
@original_filename="b4lROOR.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"user[image][]\"; filename=\"b4lROOR.jpg\"\r\nContent-Type: image/jpeg\r\n">]},
"commit"=>"Upload",
"user_id"=>"15"}
を、私はまだエラーを取得しています。誰かが私のコードでやっている何かが間違っているのを見ていますか?どんな助けでも大歓迎です。
これはすばらしいですし、素晴らしいリファクタリングです!これを使用する際に唯一の問題は私のフォームにあります。画像が追加されていないというエラーが表示されています。もう少しデバッグしてお知らせします –