简体   繁体   English

Rails-NoMethodError-未定义的方法“名称”,为nil:NilClass

[英]Rails - NoMethodError - undefined method `name' for nil:NilClass

I'm new to rails and recently noticed an error on one of my show views while working on a project for school. 我是Rails的新手,最近在从事学校项目时发现其中一个显示视图出错。 The error has to do with the name method not being defined yet I'm unsure how to remedy this. 该错误与尚未定义名称方法有关,但我不确定如何解决此问题。 Help would be much appreciated! 帮助将不胜感激! The error I receive is: 我收到的错误是:

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>

Relevant files include.. 相关文件包括..

topics/show.html.erb 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 post.rb

class Post < ActiveRecord::Base
  has_many :comments
  belongs_to :user
  belongs_to :topic

  default_scope { order('created_at DESC') }
end

topic.rb topic.rb

class Topic < ActiveRecord::Base
  has_many :posts
end

posts_controller.rb 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

topics_controller.rb 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

you are not setting the user when creating a post, Im not sure about the implementation of your authorize method. 您在创建帖子时未设置user ,我不确定您的authorize方法的实现。 However, 然而,

it should be something like 它应该像

#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

your post has no user. 您的帖子没有用户。 you have to check why your post not saved the user. 您必须检查为什么帖子没有保存用户。 to prevent this error you have to check, user exist in post. 为了防止出现此错误,您必须检查用户是否在帖子中。

i would prefer to use delegate and follow the law of demeter 我宁愿使用delegate并遵守得law of demeterlaw of demeter

post.rb 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

after this, you can receive the users name from the post like post.user_name and it will be forwarded to the name attribute of the user object. 之后,您可以从帖子中接收用户名,例如post.user_name ,它将被转发到用户对象的name属性。

and change in your show.html 并更改您的show.html

<%= post.user.name %>

to

<%= post.user_name %>

additional: please do not use default_scope. 其他:请不要使用default_scope。 if you dont need the order() you always have to unscope your queries. 如果您不需要order() ,则始终必须unscope查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoMethodError,未定义的方法“名称”,为nil:NilClass -rails 3 - NoMethodError, undefined method `name' for nil:NilClass -rails 3 Rails 4-rake db:seed NoMethodError:nil:NilClass的未定义方法“名称” - Rails 4 - rake db:seed NoMethodError: undefined method `name' for nil:NilClass Rails + CarrierWave:NoMethodError:nil的未定义方法`name&#39;:NilClass - Rails + CarrierWave: NoMethodError: undefined method `name' for nil:NilClass Rails 6 Production Active Storage NoMethodError(nil:NilClass 的未定义方法“名称”) - Rails 6 Production Active Storage NoMethodError (undefined method `name' for nil:NilClass) Rails4:NoMethodError /未定义的方法&#39;*&#39;为nil:NilClass - Rails4: NoMethodError / undefined method `*' for nil:NilClass Rails 6-NoMethodError(nil:NilClass的未定义方法“ titleize”) - Rails 6 - NoMethodError (undefined method `titleize' for nil:NilClass) Rails 4:NoMethodError:nil的未定义方法`each&#39;:NilClass - Rails 4: NoMethodError: undefined method `each' for nil:NilClass Rails DateTime-NoMethodError(nil:NilClass的未定义方法“ []”) - Rails DateTime - NoMethodError (undefined method `[]' for nil:NilClass) Ruby On Rails - NoMethodError:nil:NilClass 的未定义方法“[]” - Ruby On Rails - NoMethodError: undefined method `[]' for nil:NilClass NoMethodError:nil的未定义方法&#39;type&#39;:Rails中的NilClass - NoMethodError: Undefined method 'type' for nil:NilClass in Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM