简体   繁体   English

Rails:未定义的方法“id”为 nil:NilClass 错误

[英]Rails: Undefined method `id' for nil:NilClass error

I am currently trying to make a tennis website using RoR.我目前正在尝试使用 RoR 制作网球网站。 This website has a backend and currently has two tables in the database, users and events.这个网站有一个后端,目前在数据库中有两个表,用户和事件。 I am trying to make a has_many relationship between the two tables and then make a form that lets the users sign up for these different events.我试图在两个表之间建立 has_many 关系,然后创建一个表单,让用户注册这些不同的事件。 I am pretty close but I keep running into this error in my view:我非常接近,但在我看来,我一直遇到这个错误:

    undefined method `id' for nil:NilClass

   <%= link_to event.title, event %>
  | <%= link_to "delete", event, method: :delete %>

   <%= form_for(current_user.relationships.build(event_id: @user.id)) do |f| %>
   <div><%= f.hidden_field :event_id %></div>
     <%= f.submit "Sign up", class: "btn btn-large btn-primary" %>
   <% end %>

Here is my relationships controller这是我的关系控制器

    class RelationshipsController < ApplicationController

      def create
       @relationship = Relationship.new(relationship_params)
       @user = User.find(params[:relationships][:event_id])
       current_user.follow!(@user)
       respond_to do |format|
       format.html { redirect_to @user }
       end
     end
    end

And the relevant part of my events controller:以及我的事件控制器的相关部分:

   def new
     @event = Event.new
   end

   private

   def event_params
     params.require(:event).permit(:title, :Venue, :Time, :Date)
   end

Not sure how to fix this error, any help would be greatly appreciated.不知道如何解决这个错误,任何帮助将不胜感激。

You should initialize the @user variable您应该初始化@user 变量

def new
  @user = User.new
  @event = Event.new
end

In your controller new action you have to define @user .在您的控制器新操作中,您必须定义@user Currently this variable is empty.目前这个变量是空的。 So if you call the id method on something that's not existent you'll get the error undefined method id for Nil...所以如果你在不存在的东西上调用id方法,你会得到错误undefined method id for Nil...

For example like this:例如像这样:

def new
  @user = User.find(1)
  @event = Event.new
end

I had a similar issue when working on a Rails application.在处理Rails应用程序时,我遇到了类似的问题。 The issue was that I was calling an unnecessary instance variable in my controller.问题是我在控制器中调用了一个不必要的实例变量。

Here's it :这是它

I was calling an unnecessary @admin instance variable in log_in @admin我在log_in @admin调用了一个不必要的@admin实例变量

sessions/admins_controller.rb会话/admins_controller.rb

def create
  user = User::Admin.find_by(email: params[:sessions_admin][:email].downcase)
  if user && user.authenticate(params[:sessions_admin][:password])
    log_in @admin
    format.html { redirect_to users_admin_url(user), notice: 'Admin was successfully logged in.' }
    format.json { render :show, status: :created, location: users_admin_url(user) }
  else
    format.html { render :new }
    format.json { render json: @admin.errors, status: :unprocessable_entity }
  end
end

But error message was being flagged by rails in a corresponding helper file that I included in my application_controller file.:但是错误消息在我包含在application_controller文件中的相应帮助文件中被 rails 标记。:

sessions/admins_helper.rb会话/admins_helper.rb

module Sessions::AdminsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end
end

I simply had to modify the method call in the create action in sessions/admins_controller.rb from:我只需要修改sessions/admins_controller.rb中的create 操作中的方法调用

log_in @admin

to

log_in user

That's all.就这样。

I hope this helps我希望这有帮助

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

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM