回形针4和ruby on rails 4的问题

时间:2014-02-06 07:26:29

标签: ruby-on-rails paperclip

我正在学习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

ħ

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

我得到了params:

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"}

1 个答案:

答案 0 :(得分:1)

您需要修改这些区域:


强大的参数

#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