簡體   English   中英

回形針4和ruby on rails 4問題

[英]paperclip 4 and ruby on rails 4 problems

我正在學習Rails4,並想創建一個相冊管理應用

我選擇回形針來做到這一點。

但是上傳圖片總是失敗,有人可以幫我嗎?

Photo Controller-用於從我的upload_test方法接收帖子的上傳方法

class PhotosController < ApplicationController
  before_action :set_photo, only: [:show, :edit, :update, :destroy]

  # GET /photos
  # GET /photos.json
  def index
    @photos = Photo.all
  end

  # GET /photos/1
  # GET /photos/1.json
  def show
  end

  # GET /photos/new
  def new
    @photo = Photo.create
  end

  # GET /photos/1/edit
  def edit
  end

  def upload
    @photo = Photo.create(photo_params)
    @photo.album_id = params[:album_id]
    @photo.avatar = params[:avatar]

    if @photo.save
      render text: "1"
    else
      render text: "0"
    end
  end

  def upload_test
    @photo = Photo.create
  end

  # POST /photos
  # POST /photos.json
  def create
    @photo = Photo.create(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 { render action: 'new' }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /photos/1
  # PATCH/PUT /photos/1.json
  def update
    respond_to do |format|
      if @photo.create(photo_params)
        format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /photos/1
  # DELETE /photos/1.json
  def destroy
    @photo.destroy
    respond_to do |format|
      format.html { redirect_to photos_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_photo
      @photo = Photo.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def photo_params
      #params.permit(:avatar)
    end
end

查看:upload_test.html.erb

<%= form_tag("/photos/upload", method: "post" ,  multipart: true) do %>
  <%= text_field_tag :album_id %>
  <%= file_field_tag :avatar %>
  <%= submit_tag("save") %>
<% end %>

型號:

class Photo < ActiveRecord::Base
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end

和我的development.rb

H

air::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  Paperclip.options[:command_path] = 'C:\Program Files\ImageMagick-6.8.8-Q8'
end

發布我得到的參數:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"02RmntOJfa92Jh1iho9UMupYHxNYG99m8R8SckY9nHY=", "album_id"=>"1", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x228fdc8 @tempfile=#<File:C:/Users/CASPER~1.HUA/AppData/Local/Temp/RackMultipart20140206-8076-1hqif1h>, @original_filename="_MG_0059.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"avatar\"; filename=\"_MG_0059.JPG\"\r\nContent-Type: image/jpeg\r\n">, "commit"=>"save"}

您需要修改以下區域:


強大的參數

#app/controllers/photos_controller.rb
def photo_params
   params.require(:photo).permit(:avatar)
end

新建/創建動作

  #app/controllers/photos_controller.rb
  def new
    @photo = Photo.new
  end

  def new
    @photo = Photo.new(photo_params)

    if @photo.save
      render text: "1"
    else
      render text: "0"
    end
  end

  private

  def photo_params
      params.require(:photo).permit(:avatar)
  end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM