当我尝试通过 Lecture 控制器的 create 方法创建“Lecture”时出现此错误。这曾经可以工作,但我继续在应用程序的其他部分工作,然后我当然回来了,当用户尝试在我的应用程序中创建讲座时,现在有些东西会抛出这个错误。
我敢肯定它只是我忽略的一些小东西(已经做了一段时间了,可能需要休息一下)......但如果有人能让我知道为什么会发生这种情况,我将不胜感激......让我知道如果我需要发布其他任何内容...谢谢!
我得到的错误
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'
我对创建新讲座的看法
意见/讲座/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>
然后我的控制器说错误来自哪里
控制器/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