【问题标题】:upload image and files padrino上传图片和文件
【发布时间】:2016-07-29 04:59:06
【问题描述】:

我想把索引图片放到我博客的帖子中 并在管理员面板的“新帖子”中有一个上传表单 表格是这样写的:

    - 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'

但我不知道我必须在函数中做什么才能保存文件。

【问题讨论】:

    标签: ruby sinatra padrino


    【解决方案1】:

    一个易于遵循的指南(假设您使用的是 activerecord。否则更改示例中的第一行)。

    1. carrierwave 添加到您的Gemfile,然后执行bundle install
    2. 生成迁移:padrino g AddImageToPosts image:string 并执行它。
    3. mount_uploader :image, Uploader 添加到您的帖子模型中。
    4. 在您的 lib 文件夹中创建一个名为 uploader.rb 的文件(或其他名称,但不要忘记在第三步更改 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
    

    【讨论】:

      猜你喜欢
      • 2012-08-23
      • 2020-01-23
      • 2016-10-10
      • 2010-11-07
      • 1970-01-01
      • 2018-06-19
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多