paperclip error while displaying images

155 Views Asked by At

i get an error

undefined method `photo' for #<Post::ActiveRecord_Relation:0x00000004ad42e8> 

my posts.index.html.erb

<%= image_tag @post.photo.url(:small) %> 

gives me the above error and if i iterate

<% @post.each do |image| %>
<%= image_tag image.photo.url %>
<% end %>

i get this new error

SQLite3::SQLException: no such column: position: SELECT "posts".* FROM "posts"   ORDER BY position ASC

here is my model, post.rb

Class Post < ActiveRecord::Base
require 'digest/md5'
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png",
:path => ":rails_root/public/assets/images/:id/:style/:basename.:extension"

validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
include PublicActivity::Model
tracked

end and my posts_controller.rb

class PostsController < ApplicationController
before_action :authenticate_user!
def index
    #@hotels = hotels.new()
    @activities = PublicActivity::Activity.order("created_at desc")  

    @post = Post.order('position ASC')
  respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @picture }
end
end
def show
    @post = Post.find(params[:id])
    respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @picture }
end
end

def new
    @post = Post.new
  respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @picture }
end
end
def create
    #@post = Post.create( user_params )
    @post = Post.new(params[:posts].permit(:hotels, :photo, :number, :price, :guest_house, :restaurant,:lodges, :description, :region))
    if @post.save
        redirect_to(:controller => 'homes',:action=> 'index')
    else
        render('new')
    end
end
def edit
    @post = Post.find(params[:id])
end
def update
    @post = Post.find(params[:id])
    @post = Post.update_attributes()
end
def delete
    @post = Post.find(params[:id])
end
#def user_params
#params.require(:post).permit(:photo)
#end

end

i also have the public activity gem installed on my app and when i try to display the photos using

<%= link_to activity.trackable.photo %>

i get a link to where the photo was saved but not the actual image as shown below. help me out enter image description here

1

There are 1 best solutions below

0
On

Ah I finally realized my mistake. i used the link_to tag instead of the image_tag