我试图使用paperclip在posts索引中显示用户的图像,但我得到的只是一个“缺失”的链接。
我正在使用Devise进行身份验证。 还有用于图片上传的回形针。 这是令人沮丧的,因为它对我来说有些时间,但是occaisonaly给我带来了像这样的烦人的错误。 谢谢你的帮助,我已经被困在这里几天了。
这是我的帖子索引:(错误是在第8行引发)
<% @posts.each do |post| %> <ul> <li> <div class="well well-large"> <div class="media"> <a class="pull-left" href="#"> <%= image_tag current_user.image.url(:thumb) %> </a> <div class="media-body"> <h4 class="media-heading"><%= post.title %></h4> <%= post.content %> <!-- Nested media object --> <div class="media"> </div> </div> </div> </div> <% end %> </li> class User < ActiveRecord::Base has_many :posts has_attached_file :image, :styles => { :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/ # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable endPost.rb
class Post < ActiveRecord::Base belongs_to :user end这是我的帖子控制器中的post_params:
private def post_params params.require(:post).permit(:title, :content) endI am trying to use paperclip to show a user's image in the posts index, but all I get is a "missing" link in its place.
I am using Devise for authentication. And paperclip for image upload. Which is frustrating since it works some of the time for me, but occaisonaly brings me annoying errors like this. Thanks for the help, I've been stuck here a couple days now.
Here is my posts index: (error is thrown on 8th line)
<% @posts.each do |post| %> <ul> <li> <div class="well well-large"> <div class="media"> <a class="pull-left" href="#"> <%= image_tag current_user.image.url(:thumb) %> </a> <div class="media-body"> <h4 class="media-heading"><%= post.title %></h4> <%= post.content %> <!-- Nested media object --> <div class="media"> </div> </div> </div> </div> <% end %> </li> class User < ActiveRecord::Base has_many :posts has_attached_file :image, :styles => { :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/ # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable endPost.rb
class Post < ActiveRecord::Base belongs_to :user endHere are my post_params from my posts controller:
private def post_params params.require(:post).permit(:title, :content) end最满意答案
从你得到的错误中可以看出这一点。 看看这一行:
<a class="pull-left" href="#"> <%= image_tag current_user.image.url(:thumb) %> </a>这里你的current_user是nil ,因此你得到:
nil的未定义方法`image':NilClass
这意味着您正在请求此方法并在用户未登录时加载此视图,即未进行任何用户会话。 请检查你的PostsController : before_filter :authenticate_user! 或者在访问PostsController的索引方法之前尝试登录。
It's quite apparent from error that you're getting. Look at the line:
<a class="pull-left" href="#"> <%= image_tag current_user.image.url(:thumb) %> </a>Here your current_user in nil, and hence you get:
undefined method `image' for nil:NilClass
Which means that you're requesting this method and loading this view when the user isn't logged in i.e. no user session has been made. Please put a check in your PostsController: before_filter :authenticate_user! or try to sign in before you access PostsController's index method.
更多推荐
发布评论