Ruby on rails nil的未定义方法:NilClass。Can';t访问用户';从ROR中的另一个控制器调用时的名称或任何其他数据
当我试图从文章控制器获取评论的用户名时,我得到了一个“nil:NilClass的未定义方法'name'。我试着从comments控制器发出同样的请求,效果很好Ruby on rails nil的未定义方法:NilClass。Can';t访问用户';从ROR中的另一个控制器调用时的名称或任何其他数据,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,当我试图从文章控制器获取评论的用户名时,我得到了一个“nil:NilClass的未定义方法'name'。我试着从comments控制器发出同样的请求,效果很好 NoMethodError in Articles#show Showing C:/xampp/htdocs/app/views/comments/_show.html.erb where line #2 raised: undefined method `name' for nil:NilClass 我正在使用ruby-V2.
NoMethodError in Articles#show
Showing C:/xampp/htdocs/app/views/comments/_show.html.erb where line #2 raised:
undefined method `name' for nil:NilClass
我正在使用ruby-V2.1.5和rails-V4.2.2
以下是我的控制器代码:
class CommentsController < ApplicationController
before_filter :logged_in_user, only: [:create, :destroy]
def show
@comments= Comment.all
end
def new
@comment = Comment.new
end
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
@comment.user_id = current_user.id
if @comment.save
flash[:success] = "Comment created!"
redirect_to article_path(@article)
end
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
模型更新
评论:
class注释{顺序(在::desc创建)}
验证:body,presence:true
结束
第条:
class Article < ActiveRecord::Base
belongs_to :category
belongs_to :user
belongs_to :league
has_many :comments, :dependent => :destroy
default_scope -> { order(created_at: :desc) }
mount_uploader :front_picture, ArticleFrontUploader
validates :user_id, presence: true
validates :category_id, presence: true
validates :content, presence: true
#validate :picture_size
private
# Validates the size of an uploaded picture.
def picture_size
if front_picture.size > 5.megabytes
errors.add(:front_picture, "should be less than 5MB")
end
end
end
类文章:销毁
默认范围->{顺序(在::desc创建)}
挂载上传器:前图片,文章前上传器
验证:用户id,状态:true
验证:类别id,状态:true
验证:内容,状态:true
#验证:图片大小
私有的
#验证上载图片的大小。
def图片大小
如果前端图片大小>5.MB
错误。添加(:正面图片,“应小于5MB”)
结束
结束
结束
用户相关安全
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :articles, dependent: :destroy
has_many :comments, dependent: :destroy
...
class用户
谢谢 第一步:建立一个局部模型
渲染局部变量时,可以尝试命名局部变量。例如,在/articles/_show.html.erb
中,您可以在(当前)渲染行中输入以下内容:
<%= render partial: 'comments/show', locals: {comments: @comments} %>
这是有道理的,因为如果没有注释,为什么要显示注释内容?请注意,如果您计划使用一个表单来输入注释,请将该表单放置在语句之外,除非语句下方显示部分注释
第二步:编辑关联
我不知道您希望如何准确地设置它,因此我将不提供复制/粘贴代码,而只是解释一下。在大多数情况下,评论并不是孤立的,它们总是属于某个东西。评论直接属于一篇文章。在您的模型中,您的关联应该有一个通过文章发表评论的用户。根据您提供的代码,我相信等级如下:
unless comments.empty?
User
Article
Comment
你需要一个用户来制作一篇文章。你需要一篇文章来发表评论。虽然要求用户发表评论,但这不是直接关联。同样,我不会向您提供确切的代码,因为您可能希望以稍微不同的方式处理此问题,但思路相同。错误出现在注释视图中的\u show.html.erb
。注释只有一个用户id属性。因此,如果调用@comment.user
,则不会显示任何内容,因为注释没有user属性。您必须通过用户id
<% @comments.each do |c| %>
<h1><%= User.find_by_id(c.user_id).name %></h1>
<% end %>
试试这个。它将解决问题。将局部变量传递给部分
<%= render partial: 'comments/show', locals: { comments: @comments } %>
注释/_show.html.erb
<% comments.each do|c| %>
<h1><%= c.try(:user).try(:name) %></h1>
<% end %>
Hi@onebree谢谢你的帮助!但这样做我得到了:未定义的方法“user”for#如果您不介意的话,我可以稍后帮您解决。我认为问题在于模型关联,你应该显示它而不是模式,在我看来,关联在某个地方被破坏了,但是我可以得到对象本身,我可以链接到用户。我只是无法从评论中访问用户表信息,这就是为什么我说要发布文章的模型/comment/user和您命名的关联。@NachPD我今晚将查看所有内容。我当然认为局部/局部是必要的,但问题在于你的联想。您是否碰巧在Rails指南教程中这样做?我建议在我不在的时候,按照这个建议做进一步的建议。您的大部分代码类似于指南的教程,但这并不可取,因为您希望视图中的逻辑尽可能少。您希望在控制器中进行一次所有查询以缓存它们,因为现在此查询搜索所有用户以找到一个匹配项。这很正确,但我认为这是最简单的方法。感谢@prashant402为我工作!但为什么我们要用try访问?在模型中,我在完成此评论后发布它,评论属于用户,所以我应该可以访问用户的数据,对吗?谢谢你的帮助,但为什么试着工作,而简单的电话没有?谢谢,为了让这篇文章更具可读性,你需要对它进行大量清理。如果可能,使用pastebin或gist。
unless comments.empty?
User
Article
Comment
<% @comments.each do |c| %>
<h1><%= User.find_by_id(c.user_id).name %></h1>
<% end %>
<%= render partial: 'comments/show', locals: { comments: @comments } %>
<% comments.each do|c| %>
<h1><%= c.try(:user).try(:name) %></h1>
<% end %>