uploading image or file in ruby on rails 4 -
here, want clear concept uploading file or image without using gemfile(ex: peperclip, carrierwave etc).
previously, did work. can upload images in "/assets/images" folder. could't see images when call in show page(there placeholder of break image). here screenshot of show page:
how can show images in show.html.erb file?
here imagescontroller.rb file:
class imagescontroller < applicationcontroller before_action :set_image, only: [:show, :edit, :update, :destroy] # /images def index @images = image.all end # /images/1 def show end # /images/new def new @image = image.new end # /images/1/edit def edit end # post /images def create @image = image.new(image_params) if params[:image].present? file = params[:image][:picture] file.open(rails.root.join('app','assets', 'images', file.original_filename), 'wb') |f| f.write(file.read) end end respond_to |format| if @image.save format.html { redirect_to @image, notice: 'image created.' } format.json { render action: 'show', status: :created, location: @image } else format.html { render action: 'new' } format.json { render json: @image.errors, status: :unprocessable_entity } end end end def update respond_to |format| if @image.update(image_params) format.html { redirect_to @image, notice: 'image updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @image.errors, status: :unprocessable_entity } end end end # delete /images/1 def destroy @image.destroy respond_to |format| format.html { redirect_to images_url } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_image @image = image.find(params[:id]) end # never trust parameters scary internet, allow white list through. def image_params params.require(:image).permit(:name) end end
here show.html.erb file:
<p><%= @image.name %></p> <p><%= image_tag @image.picture %></p>
here schema.rb file:
activerecord::schema.define(version: 20140725043842) create_table "images", force: true |t| t.string "name" t.string "picture" t.datetime "created_at" t.datetime "updated_at" end end
please tell me, if need more information project. please give me advice. want clear understand uploading image/file , show them without using gemfile.
you have upload images folder doesn't need precompiling show, assets do. try uploading them public
folder.
also might want read asset pipeline , why aren't displaying:
Comments
Post a Comment