2016-10-21 7 views
0

私は、レールアプリに取り組んでいて、画像のネストされたリソースを作っています。モデルは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"} 

を、私はまだエラーを取得しています。誰かが私のコードでやっている何かが間違っているのを見ていますか?どんな助けでも大歓迎です。

答えて

0

私は一時的な回避策を考え出しました。確かに理想的ではない、誰かが良い提案をしているなら、私はそれを聞いてみたい。しかし、ここに私は現在働いているものがあります。

def create 
    pic = nil 
    if params[:user]['image'] 
    params[:user]['image'].each do |image| 
     pic = @resource.pictures.create(image: image, imageable_type: @resource, imageable_id: @resource.id) 
    end 
    if pic.save 
     redirect_to :back 
     flash[:success] = 'Image Saved!' 
    else 
     redirect_to :back 
     flash[:danger] = "#{pic.errors.full_messages.to_sentence}" 
    end 
    else 
    flash[:danger] = "Picture file not found" 
    redirect_to :back 
    end 
end 
1

問題は、フォームがキーuserを持っていながら、あなたのpicture_params方法はpictureルートのparamキーを必要とすることです。

private 
    def set_resource! 
    @resource = resource_class.find(params[param_key + "_id"]) 
    end 

    def resource_class 
    @resource_class ||= [Gym, User, Location, Product].detect do |c| 
     params[ param_key(c) + "_id" ] 
    end 
    end 

    def param_key(klass = resource_class) 
    ActiveModel::Naming.param_key(klass) 
    end 

    def picture_params 
    params.require(param_key).permit(:image) 
    end 

代わりのklass.name.underscore我々はActiveModel::Namingを使用するレールはルート、のparamsまたは翻訳のようなもののためにモデルを使用する方法を割り出し方法です:

は、いくつかのリファクタリングを行うことができます。

+0

これはすばらしいですし、素晴らしいリファクタリングです!これを使用する際に唯一の問題は私のフォームにあります。画像が追加されていないというエラーが表示されています。もう少しデバッグしてお知らせします –

関連する問題