Paperclip in Rails 4 - Strong Parameters Forbidden Attributes Error

前端 未结 2 769
北海茫月
北海茫月 2020-12-16 19:21

Having a problem with a Paperclip upload in Rails 4 - failing on ForbiddenAttributesError (strong parameters validation). Have the latest paperclip gem and latest rails 4 g

相关标签:
2条回答
  • 2020-12-16 19:28

    It looks like your first one should have worked. This is what I use for my projects.

    class GalleriesController < ApplicationController
    
      def new
        @gallery = Gallery.new
      end
    
      def create
        @user.galleries.new(gallery_params)
      end
    
      private
    
      #note cover_image is the name of paperclips attachment filetype(s)
      def gallery_params
        params.require(:gallery).permit(:cover_image)
      end
    end
    
    0 讨论(0)
  • 2020-12-16 19:31

    I understand what happened here now - and will leave this up in the hope it helps someone else. I was porting code from a rails 3 project and missed the line that created the image:

    @image = current_user.images.new(params[:image])
    

    In rails 4 this is incorrect (I beleive). I updated to

    @image = current_user.images.new(image_params)
    

    and that solved my problem.

    0 讨论(0)
提交回复
热议问题