2016-06-10 61 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