Home Ask Login Register

Developers Planet

Your answer is one click away!

aniket February 2016

Rails fetch image and display in my index view

I am trying to build a rails application in which I am uploading image using carrierwave .After uploading now i want to retrieve that image and display in my index page but i don't know how to specify the path of the image or how to retrieve that image and show it in view.Also i want to display the details like title and description which I 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 } do |f| %>
    <% if @upload.errors.any? %>
        <div id="errors">
            <h2><%= pluralize(@upload.errors.count, "error") %> prevented this post from saving:</h2>
            <ul>
                <% @upload.errors.full_messages.each do |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 do |f| %>                          

Answers


Andrey Konoplenko February 2016

You should do something like this in your uploads/index.html.erb file:

 <%= image_tag(f.tageline.url) %>

int the same way you can display other fields:

 <%= f.title %>
 <%= f.description %>

Post Status

Asked in February 2016
Viewed 3,189 times
Voted 10
Answered 1 times

Search




Leave an answer


Quote of the day: live life