2014-02-06 51 views
1

我学习rails4并想创建一个相册管理应用回形针4和Ruby on Rails 4个问题

我选择回形针做到这一点。

但它总是无法上传图像,有人可以帮我吗?

照片控制器 - 从我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 

和我的发展。 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 

post数据,我得到:

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

你需要修改这些方面:


强PARAMS

#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 
+0

我已经尝试过,但没有奏效 – peterlawn

+0

那你试着和你有什么错误? –

+0

终于,我把它改回3.5.3版本,所有的错误消失了...... O_o – peterlawn