carrierwave - Rails fetch image and display in my index view -
i trying build rails application in uploading image using carrierwave .after uploading want retrieve image , display in index page don't know how specify path of image or how retrieve image , show in view.also want display details title , description have uploaded
[uploads_controller.rb]
class uploadscontroller < applicationcontroller before_action :authenticate_user! def index @uploads=upload.all end def new @upload=upload.new end def create @upload=upload.new(params_abc) if @upload.save redirect_to @upload else render 'new' end end def show @post = find_params end private def params_abc params.require(:upload).permit(:title,:description,:tageline) end def find_params post.find(params[:id]) end end
[uploads/new.html.erb]
<%= form_for @upload,html: { multipart: true } |f| %> <% if @upload.errors.any? %> <div id="errors"> <h2><%= pluralize(@upload.errors.count, "error") %> prevented post saving:</h2> <ul> <% @upload.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <%= f.label :title %><br> <%= f.text_field :title %><br> <br> <%= f.label :description %><br> <%= f.text_field :description %><br> <br> <%= f.label :tageline %><br> <%= f.file_field :tageline%><br> <br> <%= f.submit %> <% end %>
[uploads/index.html.erb]
<% @uploads.each |f| %> <div id="page_wrapper"> <div id="profile_image"> <%= f.image "uploadss/post/"%> </div> <div id="content_link"> <div id="content"> <p>being richest man in cemetery doesn't matter me. going bed @ night saying we've done wonderful, that's matters me. </p> </div> <div id="link"> <button type="button" >read more</button> </div> <div> </div> <br> <% end %>
[migration]
class createuploads < activerecord::migration def change create_table :uploads |t| t.string :title, null: false t.string :description, null: false t.string :tageline, null: false t.timestamps end end end
[upload.rb]
class upload < activerecord::base validates :description, presence: true validates :title, presence: true validates :tageline, presence: true mount_uploader :tageline, tagelineuploader end
[uploader]
# encoding: utf-8 class tagelineuploader < carrierwave::uploader::base # include rmagick or minimagick support: # include carrierwave::rmagick # include carrierwave::minimagick # choose kind of storage use uploader: storage :file # storage :fog # override directory uploaded files stored. # sensible default uploaders meant mounted: def store_dir "uploadss/post/#{model.id}" end # provide default url default if there hasn't been file uploaded: # def default_url # # rails 3.1+ asset pipeline compatibility: # # actioncontroller::base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end # process files uploaded: # process :scale => [200, 300] # # def scale(width, height) # # # end # create different versions of uploaded files: # version :thumb # process :resize_to_fit => [50, 50] # end # add white list of extensions allowed uploaded. # images might use this: # def extension_white_list # %w(jpg jpeg gif png) # end # override filename of uploaded files: # avoid using model.id or version_name here, see uploader/store.rb details. # def filename # "something.jpg" if original_filename # end end
you should in uploads/index.html.erb file:
<%= image_tag(f.tageline.url) %>
int same way can display other fields:
<%= f.title %> <%= f.description %>
Comments
Post a Comment