2016-06-10 90 views
0

我希望把索引圖像到我的博客帖子 並在管理員的面板 在「新崗位」上傳表單的形式是這樣寫的:上傳圖片和文件padrino

- error = @post.errors.include?(:file) 
%fieldset.control-group{:class => error ? 'has-error' : ''} 
    =f.label :file, :class => 'control-label' 
    .controls 
    =f.file_field :file ,:name => 'file' 



- error = @post.errors.include?(:title) 
%fieldset.control-group{:class => error ? 'has-error' : ''} 
    =f.label :title, :class => 'control-label' 
    .controls 
    =f.text_field :title, :class => 'form-control input-large input-with-feedback', :autofocus => true 
    %span.help-inline=error ? f.error_message_on(:title, :class => 'text-error') : pat(:example) 
- error = @post.errors.include?(:body) 
%fieldset.control-group{:class => error ? 'has-error' : ''} 
    =f.label :body, :class => 'control-label' 

    .controls 
    ~f.text_area :body, :class => 'form-control input-large input-with-feedback' 
    %span.help-inline=error ? f.error_message_on(:body, :class => 'text-error') : pat(:example) 

.form-actions 
    =f.submit pat(:save), :class => 'btn btn-primary' 
      
    =f.submit pat(:save_and_continue), :class => 'btn btn-info', :name => 'save_and_continue' 
      
    =link_to pat(:cancel), url(:posts, :index), :class => 'btn btn-default' 

但我不不知道我在保存文件的功能上必須做些什麼。

回答

0

一個易於使用的指南(假設您使用activerecord,否則更改第一行的示例)。

  1. carrierwave添加到您的Gemfile中,然後執行bundle install
  2. 生成遷移:padrino g AddImageToPosts image:string並執行它。
  3. mount_uploader :image, Uploader添加到您的發佈模型。
  4. 裏面你lib文件夾中創建一個文件,命名爲uploader.rb(或什麼,但這時不要忘記在第3步改變Uploader
  5. 添加lines from 7 to 83到uploader.rb(別忘了取消註釋行,並修復他們讓他們符合你的需求)。

瀏覽管理員,單擊瀏覽按鈕進行文件上傳 - 從文件系統中選擇一個文件 - 完成。

例如(步驟3)

require 'carrierwave/orm/activerecord' 
class Post < ActiveRecord::Base 
    belongs_to :category 
    mount_uploader :image, Uploader 
    ... 
end