Rails 4: Receiving the following error: First argument in form cannot contain nil or be empty
Rails 4: Receiving the following error: First argument in form cannot contain nil or be empty
我在我的一个项目中收到以下错误:表单中的第一个参数不能包含 nil 或为空。我正在尝试为我的代码创建一个编辑页面。 Rails 相当新,尝试在没有脚手架的情况下学习。
控制器:
class BooksController < ApplicationController
def new
@book = Book.new
@authors = Author.all
end
def edit
@book = Book.find(params[:id])
end
def show
#Notice how the @books is plural here.
@books = Book.all
@authors = Author.all
#@books = Book.where(id: params[:id])
end
#Create method will save new entries
def create
@book = Book.new(book_params)
@authors = Author.all
if @book.save
flash[:success] = "Book Added to Databse!"
redirect_to @book
else
render 'new'
end
end
private
#Note that this method will go up into the create method above.
def book_params
params.require(:book).permit(:title, :pub_date, :publisher, :author_id)
end
end
模型页面:(用于书籍)
class Book < ActiveRecord::Base
validates :title, :pub_date, :publisher, presence: true
validates :title, uniqueness: true
belongs_to :author
end
模型页面:(对于作者)
class Author < ActiveRecord::Base
validates :name, presence: true
validates :name, uniqueness: true
has_many :books
end
编辑页面:
<h1>Update a book entry</h2>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@book) do |f| %> **ERROR SEEMS TO BE RIGHT HERE!!!**
<%= render 'form' %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :pub_date %>
<%= f.text_field :pub_date, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :publisher %>
<%= f.text_field :publisher, class: 'form-control' %><br />
</div>
<div class="form-group">
<%= f.select(:author_id,
@authors.collect {|a| [ a.name, a.id ]},
{:include_blank => 'Please select an author'},
class: "form-control") %><br />
</div>
<%= f.submit 'Save Changes', class: "btn btn-primary" %>
<% end %>
</div>
</div>
渲染表单页面(_form.html.erb)
<% if @book.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
<h2><%= pluralize(@book.errors.count, "error") %>
prohibited this entry from being saved:</h2>
<ul>
<% @book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
显示页面:
<div class="move">
<h1>Showing Book Titles:</h1>
</div><br />
<div class="row">
<% @books.each do |book| %>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-body">
<h2><%= book.title %></h2>
<h2><%= book.publisher %></h2>
<h2><%= book.pub_date %></h2>
<h2><%= book.author.name %></h2>
<h2><%= link_to "Edit", edit_path, class: "btn btn-primary" %>
</div>
</div>
</div>
<% end %>
这是我的日志,告诉我出了什么问题:
Started GET "/edit" for ::1 at 2015-08-14 16:49:17 -0400
Processing by BooksController#edit as HTML
Rendered books/edit.html.erb within layouts/application (2.2ms)
Completed 500 Internal Server Error in 9ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (First argument in form cannot contain nil or be empty):
3: <div class="row">
4: <div class="col-md-6 col-md-offset-3">
5:
6: <%= form_for(@book) do |f| %>
7: <%= render 'form' %>
8:
9: <div class="form-group">
app/views/books/edit.html.erb:6:in `_app_views_books_edit_html_erb___525891009649529081_70260522100960'
我会说我已经从我的数据库中删除了前 14 本书,所以第一本书从 ID 14 开始。不确定这是否重要。
最后,我尝试在编辑方法中将所有这些不同的实例变量添加到我的控制器中:
#@book = Book.where(id: params[:id])
#@book = Book.find_by_id(params[:id])
#@book = Book.all
#@book = Book.find_by_id(params[:id])
#book = Book.new(book_params)
#When I use the two below lines,
there are no error pages but create a new entry.
#@book = Book.new
#@authors = Author.all
任何帮助将不胜感激!感谢您的宝贵时间!!!
此错误意味着 form_for
的第一个参数是 nil
值(在本例中为 @book
)。大多数时候我都看到过这种情况,这是由于格式错误的控制器操作造成的,但这里看起来并非如此。据我所知,这是两件事之一:
您正在尝试编辑一本不存在的图书。在决定呈现表单之前对其进行 .nil?
检查,然后呈现错误消息(或重定向)。
您的路线已损坏,edit
操作未呈现 edit
视图。这很可能不是情况。
编辑:
使用 show
的模板更新后,这看起来像是您的问题:
<%= link_to "Edit", edit_path, class: "btn btn-primary" %>
我发现这有两个问题(尽管我需要查看 rake routes
的输出来验证)。首先,您需要向编辑路径传递一个参数(没有它们,您的参数从何而来?)。其次,默认路由为 edit_book_path
.
试试这个:
<%= link_to "Edit", edit_book_path(book), class: "btn btn-primary" %>
假设图书 ID 14 在您的数据库中,如果您使用 resources :books
(文档 here)之类的内容创建它,您应该能够导航到 localhost:3000/books/14/edit
。如果这不起作用,要么是您的路线定义不正确,要么是您的数据库中不存在 ID 为 14 的书籍。
在您的显示视图中,将 link_to
行更改为:
<%= link_to 'Edit', edit_book_path(book), class: "btn btn-primary" %>
所以两个变化:
- 同样,假设书是 Restful 资源,当您 运行
rake_routes
时,您应该看到要编辑的路径是 edit_book_path
.
- 您需要将路径传递给
book
实例,以便 Rails 知道您要编辑哪个对象。
我找到了正确的语法 here。
希望对您有所帮助。
我在我的一个项目中收到以下错误:表单中的第一个参数不能包含 nil 或为空。我正在尝试为我的代码创建一个编辑页面。 Rails 相当新,尝试在没有脚手架的情况下学习。
控制器:
class BooksController < ApplicationController
def new
@book = Book.new
@authors = Author.all
end
def edit
@book = Book.find(params[:id])
end
def show
#Notice how the @books is plural here.
@books = Book.all
@authors = Author.all
#@books = Book.where(id: params[:id])
end
#Create method will save new entries
def create
@book = Book.new(book_params)
@authors = Author.all
if @book.save
flash[:success] = "Book Added to Databse!"
redirect_to @book
else
render 'new'
end
end
private
#Note that this method will go up into the create method above.
def book_params
params.require(:book).permit(:title, :pub_date, :publisher, :author_id)
end
end
模型页面:(用于书籍)
class Book < ActiveRecord::Base
validates :title, :pub_date, :publisher, presence: true
validates :title, uniqueness: true
belongs_to :author
end
模型页面:(对于作者)
class Author < ActiveRecord::Base
validates :name, presence: true
validates :name, uniqueness: true
has_many :books
end
编辑页面:
<h1>Update a book entry</h2>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@book) do |f| %> **ERROR SEEMS TO BE RIGHT HERE!!!**
<%= render 'form' %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :pub_date %>
<%= f.text_field :pub_date, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :publisher %>
<%= f.text_field :publisher, class: 'form-control' %><br />
</div>
<div class="form-group">
<%= f.select(:author_id,
@authors.collect {|a| [ a.name, a.id ]},
{:include_blank => 'Please select an author'},
class: "form-control") %><br />
</div>
<%= f.submit 'Save Changes', class: "btn btn-primary" %>
<% end %>
</div>
</div>
渲染表单页面(_form.html.erb)
<% if @book.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
<h2><%= pluralize(@book.errors.count, "error") %>
prohibited this entry from being saved:</h2>
<ul>
<% @book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
显示页面:
<div class="move">
<h1>Showing Book Titles:</h1>
</div><br />
<div class="row">
<% @books.each do |book| %>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-body">
<h2><%= book.title %></h2>
<h2><%= book.publisher %></h2>
<h2><%= book.pub_date %></h2>
<h2><%= book.author.name %></h2>
<h2><%= link_to "Edit", edit_path, class: "btn btn-primary" %>
</div>
</div>
</div>
<% end %>
这是我的日志,告诉我出了什么问题:
Started GET "/edit" for ::1 at 2015-08-14 16:49:17 -0400
Processing by BooksController#edit as HTML
Rendered books/edit.html.erb within layouts/application (2.2ms)
Completed 500 Internal Server Error in 9ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (First argument in form cannot contain nil or be empty):
3: <div class="row">
4: <div class="col-md-6 col-md-offset-3">
5:
6: <%= form_for(@book) do |f| %>
7: <%= render 'form' %>
8:
9: <div class="form-group">
app/views/books/edit.html.erb:6:in `_app_views_books_edit_html_erb___525891009649529081_70260522100960'
我会说我已经从我的数据库中删除了前 14 本书,所以第一本书从 ID 14 开始。不确定这是否重要。
最后,我尝试在编辑方法中将所有这些不同的实例变量添加到我的控制器中:
#@book = Book.where(id: params[:id])
#@book = Book.find_by_id(params[:id])
#@book = Book.all
#@book = Book.find_by_id(params[:id])
#book = Book.new(book_params)
#When I use the two below lines,
there are no error pages but create a new entry.
#@book = Book.new
#@authors = Author.all
任何帮助将不胜感激!感谢您的宝贵时间!!!
此错误意味着 form_for
的第一个参数是 nil
值(在本例中为 @book
)。大多数时候我都看到过这种情况,这是由于格式错误的控制器操作造成的,但这里看起来并非如此。据我所知,这是两件事之一:
您正在尝试编辑一本不存在的图书。在决定呈现表单之前对其进行
.nil?
检查,然后呈现错误消息(或重定向)。您的路线已损坏,
edit
操作未呈现edit
视图。这很可能不是情况。
编辑:
使用 show
的模板更新后,这看起来像是您的问题:
<%= link_to "Edit", edit_path, class: "btn btn-primary" %>
我发现这有两个问题(尽管我需要查看 rake routes
的输出来验证)。首先,您需要向编辑路径传递一个参数(没有它们,您的参数从何而来?)。其次,默认路由为 edit_book_path
.
试试这个:
<%= link_to "Edit", edit_book_path(book), class: "btn btn-primary" %>
假设图书 ID 14 在您的数据库中,如果您使用 resources :books
(文档 here)之类的内容创建它,您应该能够导航到 localhost:3000/books/14/edit
。如果这不起作用,要么是您的路线定义不正确,要么是您的数据库中不存在 ID 为 14 的书籍。
在您的显示视图中,将 link_to
行更改为:
<%= link_to 'Edit', edit_book_path(book), class: "btn btn-primary" %>
所以两个变化:
- 同样,假设书是 Restful 资源,当您 运行
rake_routes
时,您应该看到要编辑的路径是edit_book_path
. - 您需要将路径传递给
book
实例,以便 Rails 知道您要编辑哪个对象。
我找到了正确的语法 here。
希望对您有所帮助。