ruby on rails - Paperclip: "has an extension that does not match its content" error -
i trying simple image uploading using paperclip. googled issue , seems else has much more complicated problem simple uploads. below models , controllers.
pin.rb
class pin < activerecord::base belongs_to :user has_attached_file :image validates_attachment_content_type :image, :content_type => /\aimage\/.*\z/ # validates description validates :description, presence: true validates :user_id, presence: true # validates paperclip validates_attachment :image, presence: true, content_type: { content_type: ["image/jpeg", "image/jpg", "image/png", "image/gif"]}, size: { less_than: 5.megabytes } end pin_controller.rb
class pinscontroller < applicationcontroller #before_filer authentiate users create sure logged in before doing pins, except of indexing pins non-logged on users can see them before_filter :authenticate_user!, except: [:index] before_action :set_pin, only: [:show, :edit, :update, :destroy] # /pins # /pins.json def index @pins = pin.all end # /pins/1 # /pins/1.json def show end # /pins/new def new # associate @pin current user's id @pin = current_user.pins.new end # /pins/1/edit def edit # makes sure no other user can mess without proper user id @pin = current_user.pins.find(params[:id]) end # post /pins # post /pins.json def create # associate @pin current user's id @pin = current_user.pins.new(pin_params) respond_to |format| if @pin.save format.html { redirect_to @pin, notice: 'pin created.' } format.json { render action: 'show', status: :created, location: @pin } else format.html { render action: 'new' } format.json { render json: @pin.errors, status: :unprocessable_entity } end end end # patch/put /pins/1 # patch/put /pins/1.json def update # makes sure no other user can mess without proper user id @pin = current_user.pins.find(params[:id]) respond_to |format| if @pin.update(pin_params) format.html { redirect_to @pin, notice: 'pin updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @pin.errors, status: :unprocessable_entity } end end end # delete /pins/1 # delete /pins/1.json def destroy # makes sure no other user can mess without proper user id @pin = current_user.pins.find(params[:id]) @pin.destroy respond_to |format| format.html { redirect_to pins_url } format.json { head :no_content } end end private # utilize callbacks share mutual setup or constraints between actions. def set_pin @pin = pin.find(params[:id]) end # never trust parameters scary internet, allow white list through. def pin_params params.require(:pin).permit(:description, :image) end end here think might wrong code.
first, followed 1 month rails tutorial, since watching outdated rails 3 series, have convert teaches rails 4 myself through research. may have set strong parameters wrong in pin.rb controller. added :image attribute field pin_params method so
params.require(:pin).permit(:description, :image) is right way add together :image using strong parameters? in rails 3, added :image within attr_accessible
second, in readme paperclip, says should run which convert , input line in config file
paperclip.options[:command_path] = "/usr/local/bin/" since using windows machine, got after looking answers hours
paperclip.options[:command_path] = "c:/program files/imagemagick-6.8.9-q16/" does above path looks relatively right windows?
i utilize windows , getting error. found reply here. gather, paperclip protects against spoofing getting server's os validate file's mime type. executing next command:
file -b --mime <my_file_name.extension> the problem is, standard windows command line doesn't have file command, spoofing check fails , paperclip throws error observed.
to resolve issue:
install file windows.
edit system's path variable includes "file windows" bin directory path. (for me, c:\program files (x86)\gnuwin32\bin.)
restart command line, git bash, or have because edited path variable.
confirm windows command line responds file command.
this method worked me on windows 8.1 paperclip 4.2.1.
ruby-on-rails paperclip
No comments:
Post a Comment