2013-04-22 2 views
1

첨부 파일이 필요한 프로젝트 작업 중입니다. 특히 사진을 제품 모델에 첨부해야합니다.파일을 저장했지만 읽지 못함

나는 railscast (링크 here)를 따라 갔고 대부분의 지침을 따랐다. 제품에

형태 :

class Product < ActiveRecord::Base 
    attr_accessible :brand_id, :name, :pic 

    attr_accessor :pic_file_name 
    attr_accessor :pic_content_type 
    attr_accessor :pic_file_size 
    attr_accessor :pic_updated_at 

    has_attached_file :pic 

그리고 그 결과 첨부 파일을 볼 수있는, 제품 에서 : 모델 제품에서

<div class="field"> 
    <%= f.label "Pic"%><br /> 
    <%= f.file_field :pic %> 
    </div> 

나는 다음과 결국 보기보기 :

<p> 
    <b>Pic:</b> 
    <%= image_tag @product.pic.url %> 
</p> 

이제 문제는 로그가 file_field으로 선택한 이미지가 성공적으로 업로드 된 것을 알 수에도 불구하고 있다는 것입니다 :

Started POST "/products" for 127.0.0.1 at 2013-04-22 17:10:56 -0500 
Processing by ProductsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"llIJTXKH87e7vpjX1A7Kmcz6BD2UToPvIfVd1bqmA58=", "product"=>{"brand_id"=>"1", "name"=>"asda\ 
sdsa", "pic"=>#<ActionDispatch::Http::UploadedFile:0x007f9cfdb201c8 @original_filename="3.JPG", @content_type="image/jpeg", @headers="Conten\ 
t-Disposition: form-data; name=\"product[pic]\"; filename=\"3.JPG\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/_r/d96rh\ 
0652xndcknb2txzn77r0000gn/T/RackMultipart20130422-2306-qoojw2>>}, "commit"=>"Create Product"} 
    ^[[1m^[[36m (0.1ms)^[[0m ^[[1mbegin transaction^[[0m 
    ^[[1m^[[35mSQL (76.1ms)^[[0m INSERT INTO "products" ("brand_id", "created_at", "image_content_type", "image_file_name", "image_file_size"\ 
, "image_updated_at", "name", "pic_content_type", "pic_file_name", "pic_file_size", "pic_updated_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?\ 
, ?, ?, ?, ?, ?, ?) [["brand_id", 1], ["created_at", Mon, 22 Apr 2013 22:10:56 UTC +00:00], ["image_content_type", nil], ["image_file_name"\ 
, nil], ["image_file_size", nil], ["image_updated_at", nil], ["name", "asdasdsa"], ["pic_content_type", nil], ["pic_file_name", nil], ["pic_\ 
file_size", nil], ["pic_updated_at", nil], ["updated_at", Mon, 22 Apr 2013 22:10:56 UTC +00:00]] 
[paperclip] Saving attachments. 
    ^[[1m^[[36m (0.9ms)^[[0m ^[[1mcommit transaction^[[0m 
Redirected to http://localhost:3000/products/10 
Completed 302 Found in 126ms (ActiveRecord: 77.1ms) 

실제로 항상 말한대로 "없음"이미지를 볼 수 없습니다.

이제, 공공/시스템 디렉토리를 찾고 내가 거기 모델 디렉토리뿐만 아니라 종이 클립 저장 디렉토리 트리뿐만 아니라 이미지를 볼 수 있습니다 내가 저장하려고 :

$ ls public/system/products/pics/000/000/010/original/ 
3.JPG 

내가 한

설정/초기화/paperclip.rb가

보유하고 어떤 파일에서 ImageMagick이 경로를 설정 :

Paperclip.options[:command_path] = "/Users/<my_user>/Documents/non-ios_apps/ImageMagick-6.8.1/bin/" 

그래서 이미지 은 (는)이 업로드되었지만 읽을 수 없습니다. 첨부 파일과 관련해서는 데이터베이스에 아무 것도 저장하지 않는 것과 관련이있을 수 있지만, 별도의 문제인지 또는 다른 문제인지 여부는 확실하지 않습니다.

도움을 주시면 감사하겠습니다.

감사합니다.

사용 : 레일 3.2.10 "클립을", "~> 3.0"

답변

1

그것은 포스트 요청에 업로드되는 파일처럼 보이지만 당신은 SQL에서 보면 스크립트가 실행되고, 모든 그림 속성이 nil로 설정됩니다.

모델에서 attr_accessor 행의 출처가 확실하지 않습니다. railscast 또는 현재 paperclip 문서에 언급되지 않은 부분이 있으므로 잘 모르겠습니다. 문제가 원인 일 수 있습니다.

railscast는 5 살이므로 github https://github.com/thoughtbot/paperclip의 클립 클립 프로젝트에 가서 현재 버전의 클립 클립을 설치하고 사용하는 방법을 읽어 보시기 바랍니다. 그들은 그것을 설치하고 실행하는 데 매우 간단한 연습을합니다.

+0

고마워요! 나는 모든 attr_accessor를 오류를 설정하기 위해 추가 했었습니다. 과거에는 대량 할당에 관한 것이었고, 클립 클립 마이그레이션이 불완전하거나 그런 경우라면 추가해야했습니다. – snowingheart

관련 문제