Ruby on rails 4 显示图像时出现回形针错误
我犯了一个错误Ruby on rails 4 显示图像时出现回形针错误,ruby-on-rails-4,paperclip,public-activity,Ruby On Rails 4,Paperclip,Public Activity,我犯了一个错误 undefined method `photo' for #<Post::ActiveRecord_Relation:0x00000004ad42e8> 这是我的模型,post.rb Class Post < ActiveRecord::Base require 'digest/md5' has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "1
undefined method `photo' for #<Post::ActiveRecord_Relation:0x00000004ad42e8>
这是我的模型,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
结束
还有我的帖子(u 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 %>
我得到了一个链接,指向照片保存的位置,但不是下图所示的实际图像。帮帮我啊,我终于意识到我的错误了。我使用了link_to标记而不是image_标记
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
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
<%= link_to activity.trackable.photo %>