简体   繁体   English

没有方法错误nil:NilClass的未定义方法“保存”

[英]No Method Error Undefined method 'save' for nil:NilClass

I'm getting this error when i try to create a "Lecture" via my Lecture controller's create method. 当我尝试通过我的演讲控制器的create方法创建“演讲”时出现此错误。 This used to work but i went on to work on other parts of the app & then of course i come back & something is now throwing this error when a user tries to create a Lecture in my app. 这曾经可以工作,但是我继续在应用程序的其他部分上工作,然后我当然又回来了;当用户尝试在我的应用程序中创建讲座时,现在会抛出此错误。

I'm sure its something small i'm just overlooking (been at it a while & probably need to take a break)...but I'd appreciate if someone could let me know why this is happening...let me know if i need to post anything else...thx! 我敢肯定,我只是忽略了一些小事(花了一段时间,可能需要休息一下)...但是,如果有人能让我知道发生这种情况的原因,我将不胜感激。如果我需要发布其他任何内容...谢谢!

The error I get 我得到的错误

NoMethodError in LecturesController#create

undefined method `save' for nil:NilClass
Rails.root: /Users/name/Sites/rails_projects/app_name

Application Trace | Framework Trace | Full Trace
app/controllers/lectures_controller.rb:13:in `create'

My view to create a new Lecture 我的观点,以创建一个新的讲座

views/lectures/new.html.erb views / lectures / new.html.erb

<% provide(:title, 'Start a Lecture') %>

<div class="container">
  <div class="content-wrapper">

    <h1>Create a Lecture</h1>

     <div class="row">
       <div class="span 6 offset3">
         <%= form_for(@lecture) do |f| %>
           <%= render 'shared/error_messages', :object => f.object %>
           <div class="field">
             <%= f.text_field :title, :placeholder => "What will this Lecture be named?" %>
             <%= f.text_area :content, :placeholder => "Describe this Lecture & what will be learned..." %>
           </div>
          <%= f.submit "Create this Lecture", :class => "btn btn-large btn-primary" %>
         <% end %>
       </div>
     </div>
   </div>
 </div>

Then my controller where its saying the error is coming from 然后我的控制器说错误来自哪里

controllers/lectures_controller.rb controllers / lectures_controller.rb

class LecturesController < ApplicationController
before_filter :signed_in_user, :only => [:create, :destroy]
before_filter :correct_user,   :only => :destroy

def index
end

def new
  @lecture = current_user.lectures.build if signed_in?
end

def create
  if @lecture.save
   flash[:success] = "Lecture created!"
   redirect_to @lecture
  else
   @activity_items = [ ]
   render 'new'
  end
end

def show
 @lecture = Lecture.find(params[:id])
end

def destroy
  @lecture.destroy
  redirect_to root_path
end


private

  def correct_user
    @lecture = current_user.lectures.find_by_id(params[:id])
    redirect_to root_path if @lecture.nil?
  end

You're relying on a before filter to set an instance variable for the subsequent action. 您依赖于before过滤器来为后续操作设置实例变量。 This doesn't work. 这行不通。 You have to explicitly set it in the controller action that needs it. 您必须在需要它的控制器操作中明确设置它。 The sole purpose of before filters is to filter which actions get run and which don't. before过滤器的唯一目的是过滤哪些操作可以运行,哪些不能运行。

EDIT: I misread your code. 编辑:我误读了您的代码。 I thought you were running correct_user before the create action. 我以为您在创建操作之前正在运行correct_user。 Either way, glad to know it works now! 不管哪种方式,很高兴知道它现在可以工作了!

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

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