0

i want to put index image to my blog's posts and have an upload form in 'new post 'in admin's panel the form is written like this :

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

but i don't know what i must do in functions to save file .

1 Answers1

1

An easy-to-follow guide (assuming you are using activerecord. otherwise change 1st line on example).

  1. Add carrierwave to your Gemfile, and execute bundle install.
  2. Generate a migration: padrino g AddImageToPosts image:string and execute it.
  3. Add mount_uploader :image, Uploader to your Post model.
  4. Inside your lib folder create a file, named uploader.rb (or whatever, but then do not forget to change Uploader on 3rd step.)
  5. Add lines from 7 to 83 to uploader.rb (do not forget uncomment lines, and fix them so they to match your needs).

Browse admin, click browse button for file upload - select a file from filesystem - you are done.

example (step 3)

require 'carrierwave/orm/activerecord'
class Post < ActiveRecord::Base
    belongs_to :category
    mount_uploader :image, Uploader
    ...
end
marmeladze
  • 6,468
  • 3
  • 24
  • 45