![](/img/trans.png)
[英]NoMethodError, undefined method `name' for nil:NilClass -rails 3
[英]Rails - NoMethodError - undefined method `name' for nil:NilClass
我是Rails的新手,最近在从事学校项目时发现其中一个显示视图出错。 该错误与尚未定义名称方法有关,但我不确定如何解决此问题。 帮助将不胜感激! 我收到的错误是:
NoMethodError in Topics#show
Showing /Users/Jason/code/bloccit/app/views/topics/show.html.erb where line #17 raised:
undefined method `name' for nil:NilClass
<%= link_to post.title, [@topic, post] %>
</h4>
<small>
submitted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.name %><br>
<%= post.comments.count %> Comments
</small>
</div>
相关文件包括..
topic / show.html.erb
<h1><%= @topic.name %></h1>
<% if policy(@topic).update? %>
<%= link_to "Edit Topic", edit_topic_path, class: 'btn btn-success' %>
<% end %>
<div class="row">
<div class="col-md-8">
<p class="lead"><%= @topic.description %></p>
<% @posts.each do |post| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<%= link_to post.title, [@topic, post] %>
</h4>
<small>
submitted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.name %><br>
<%= post.comments.count %> Comments
</small>
</div>
</div>
<% end %>
</div>
<div class="col-md-4">
<% if policy(Post.new).create? %>
<%= link_to "New Post", new_topic_post_path(@topic), class: 'btn btn-success' %>
<% end %>
</div>
</div>
post.rb
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
belongs_to :topic
default_scope { order('created_at DESC') }
end
topic.rb
class Topic < ActiveRecord::Base
has_many :posts
end
posts_controller.rb
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
@topic = Topic.find(params[:topic_id])
authorize @post
end
def new
@topic = Topic.find(params[:topic_id])
@post = Post.new
authorize @post
end
def create
@topic = Topic.find(params[:topic_id])
@post = Post.new(post_params)
@post.topic = @topic
authorize @post
if @post.save
flash[:notice] = "Post was saved."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
def edit
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize @post
end
def update
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize @post
if @post.update_attributes(post_params)
flash[:notice] = "Post was updated."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
topic_controller.rb
class TopicsController < ApplicationController
def index
@topics = Topic.all
authorize @topics
end
def new
@topic = Topic.new
authorize @topic
end
def show
@topic = Topic.find(params[:id])
@posts = @topic.posts
authorize @topic
end
def create
@topic = Topic.new(topic_params)
authorize @topic
if @topic.save
redirect_to @topic, notice: "Topic was saved successfully."
else
flash[:error] = "Error creating topic. Please try again."
render :new
end
end
def edit
@topic = Topic.find(params[:id])
authorize @topic
end
def update
@topic = Topic.find(params[:id])
authorize @topic
if @topic.update_attributes(topic_params)
redirect_to @topic, notice: "Topic was updated successfully."
else
flash[:error] = "Error saving topic. Please try again."
render :edit
end
end
private
def topic_params
params.require(:topic).permit(:name, :description, :public)
end
end
您在创建帖子时未设置user
,我不确定您的authorize
方法的实现。 然而,
它应该像
#assuming you have the user as `current_user`
class PostsController < ApplicationController
...
def create
@topic = Topic.find(params[:topic_id])
@post = Post.new(post_params)
@post.user = current_user
@post.topic = @topic
...
end
...
end
您的帖子没有用户。 您必须检查为什么帖子没有保存用户。 为了防止出现此错误,您必须检查用户是否在帖子中。
我宁愿使用delegate
并遵守得law of demeter
的law of demeter
post.rb
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
belongs_to :topic
delegate :name, :to :user, allow_nil: true, prefix: true
default_scope { order('created_at DESC') }
end
之后,您可以从帖子中接收用户名,例如post.user_name
,它将被转发到用户对象的name属性。
并更改您的show.html
<%= post.user.name %>
至
<%= post.user_name %>
其他:请不要使用default_scope。 如果您不需要
order()
,则始终必须unscope
查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.