0

我收到一个错误

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

我的posts.index.html.erb

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

给我上面的错误,如果我迭代

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

我收到这个新错误

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

这是我的模型,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

结束和我的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

我还在我的应用程序上安装了公共活动 gem,当我尝试使用显示照片时

<%= link_to activity.trackable.photo %>

我得到一个链接到保存照片的位置,但不是实际图像,如下所示。帮帮我在此处输入图像描述

4

1 回答 1

0

啊,我终于意识到我的错误了。我使用了 link_to 标签而不是 image_tag

于 2015-01-20T01:50:46.423 回答