使用回形针进行图像上传时出现未定义的“图像”错误?(Getting undefined 'image' errors when using paperclip for image

编程入门 行业动态 更新时间:2024-10-25 20:24:35
使用回形针进行图像上传时出现未定义的“图像”错误?(Getting undefined 'image' errors when using paperclip for image upload?)

我试图使用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 end

Post.rb

class Post < ActiveRecord::Base belongs_to :user end

这是我的帖子控制器中的post_params:

private def post_params params.require(:post).permit(:title, :content) end

I 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 end

Post.rb

class Post < ActiveRecord::Base belongs_to :user end

Here 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.

更多推荐

本文发布于:2023-07-22 20:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1223310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   回形针   错误   上传   未定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!