2012-04-02 2 views
1

초급자입니다. 그리고 나는 이미지 URL을 얻을 수 없다 (나는 방법을 모른다). https://github.com/Atlantia/tinymce-rails-imageuploadRoR tinymce-rails-imageupload URL 얻기

1.Controller

class TinymceAssetsController < ApplicationController 
    def create 
    DataFile.save(params[:file]) 

    render json: { 
    image: { 
     url: view_context.image_url(image) 
    } 
end 
} 

2.Model

class DataFile < ActiveRecord::Base 
    def self.save(file) 
    name = file.original_filename 
    #create the file path 
    path = File.join(directory, name) 
    #write the file 
    File.open(path, "wb") { |f| f.write(file.read) } 
    end 
end 

파일이 저장됩니다,하지만 난 URL을 ... 도움을 감사를 얻을하는 방법을 모르겠어요.

나는 Paperclip으로 결정했습니다. 그러나 문제가 있습니다.

class DataFile < ActiveRecord::Base 
    has_attached_file :file, styles: { medium: "300x300>", thumb: "100x100>" } 
end 

class TinymceAssetsController < ApplicationController 
    def create 
    @datafile.update_attributes(file: params[:file]) 

    render json: { 
    image: { 
     url: @datafile.file.url(:medium) 
    } 
    } 
    end 
end 

NoMethodError (전무에 대한 정의되지 않은 메서드`update_attributes 'NilClass)

과 또 다른 질문 : 나는 스타일을 지적하지 않은 경우 이 사진이 원래 크기를 가지고 있습니까?

답변

0

당신이 링크 한 보석의 근원을 보면 TinyMCE에서 파일을 저장/처리하는 것처럼 보이지 않습니다.

@datafile.update_attributes(:file => params[:file]) 

과 함께보기에서 전화 :

내가 종이 클립과 보석을 결합 권하고 싶습니다 (https://github.com/thoughtbot/paperclip)

class Datafile < ActiveRecord::Base 
    has_attached_file :file, :styles => { :medium => "300x300>", :thumb => "100x100>" } 
end 

그런 다음 당신은 그것을 update_attributes의 파일 개체를 전달할 수 있습니다

@datafile.file.url(:medium) 

직접 처리하고 싶다면 y에 저장 중입니다. 디렉토리의 파일 시스템/사용자의 원래 파일 이름 ... 아마도 당신은 주입 공격과 나쁜 파일 이름을 열어 둡니다! 직접 생성하는 경우 Dir 클래스를 사용하여 올바른 파일 이름을 찾기 위해 파일 시스템을 살펴볼 수 있습니다. 그것은, 그래서 당신의 예에서

class TinymceAssetsController < ApplicationController 
    def create 
    @datafile = DataFile.create(file: params[:file]) 


     render json: { 
     image: { 
      url: @datafile.file.url(:medium) 
     } 
     } 
    end 
end 

, 당신은 @datafile 개체를 인스턴스화되지 않습니다

하여 질문에 개정 주어진 편집

좋아는 여기 컨트롤러 코드가 어떻게 보일지입니다 무. DataFile을 데이터베이스에서 가져 오거나 인스턴스화해야합니다. 기본적으로 Paperclip은 파일 처리를 추상화하여 일반 속성과 유사합니다. 당신은 데이터베이스에 객체를 생성 할 필요가 있습니다.

+0

매우 드물게 사진을 업로드하십시오. Dir 클래스는 구현하기에 적합합니까? 고맙습니다. – user1309082

+0

Paperclip은 일반적인 파일 업로드를 처리합니다. 스타일 해시를 전달하지 않고 파일을 저장하고 검색합니다. ''Dir.entries ("mydirname")'는 디렉토리에있는 파일 이름 목록을 제공 할 것이므로 자신이 정말로하고 싶다면 하위 디렉토리에 모든 파일을 저장해야합니다 ("uploads/# {self.id}/") 데이터베이스를 건드리지 않고 파일 목록을 찾으려면 Dir.entries를 사용하십시오. 다시 한번 말하지만, 나는 적절한 업로드 관리 젬을 사용하여 정말로 강조하고 싶습니다! –

+0

조. 종이 클립으로 도울 수 있겠 니? NoMethodError (정의되지 않은 메서드 인'update_attributes 'for nil : NilClass). 고맙습니다. – user1309082

1

여기 .. ​​보석 '잠자리'0.9.12 " https://github.com/markevans/dragonfly/

1.controller

class TinymceAssetsController < ApplicationController 

    def create 
    @image = StaticImage.new(:page_image => params[:file]) 
    @image.save  
    render json: { 
     image: { 
     url: @image.page_image.url 
     } 
    }, content_type: "text/html" 
    end 
end 

2 플라이를 이용한 용액이다.모델

class StaticImage < ActiveRecord::Base 
    image_accessor :page_image 
    attr_accessible :page_image 
end 
0

나는 그것을 다음과 같은 방법을했다 :

컨트롤러 : 이미지 공공/이미지/업로드로 파일을 저장

class Image 

    def self.save(file) 
    name = file.original_filename 
    #create the file path 
    write_path = File.join(Rails.root,'public','images','upload', name) 
    return_path = File.join('upload', name) 
    #write the file 
    File.open(write_path, "wb") { |f| f.write(file.read) } 
    return_path 
    end 

end 

저장을위한

# For uploading images frm TinyMCE 

class TinymceAssetsController < ApplicationController 
    def create 
    # Take upload from params[:file] and store it somehow... 
    # Optionally also accept params[:hint] and consume if needed 

    image=Image.save(params[:file]) 
    render json: { 
     image: { 
     url: view_context.image_url(image) 
     } 
    }, content_type: "text/html" 
    end 
end 

모델 Tinymce에 의해 올바른 URL을 올바르게 삽입합니다.