我收到错误
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 %>
我获得了照片保存位置的链接,但未显示实际图像,如下所示。帮助我
答案 0 :(得分:0)
啊我终于意识到自己的错误。我使用了link_to标签而不是image_tag