Ruby Rails 4:接收到以下错误:表单中的第一个参数不能包含nil或为空
我在我的一个项目中收到以下错误:表单中的第一个参数不能包含nil或为空。我正在尝试为我的代码创建一个编辑页面。使用Rails非常新,并且尝试在没有脚手架的情况下学习 控制器:Ruby Rails 4:接收到以下错误:表单中的第一个参数不能包含nil或为空,ruby,forms,controller,ruby-on-rails-4.2,Ruby,Forms,Controller,Ruby On Rails 4.2,我在我的一个项目中收到以下错误:表单中的第一个参数不能包含nil或为空。我正在尝试为我的代码创建一个编辑页面。使用Rails非常新,并且尝试在没有脚手架的情况下学习 控制器: class BooksController < ApplicationController def new @book = Book.new @authors = Author.all end def edit @book = Book.find(params[:id]) end def s
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
任何帮助都将不胜感激!谢谢你的时间 此错误意味着的
form_的第一个参数是nil
值(在本例中为@book
)。我看到的大多数情况下,这是由于控制器动作的错误,但这里的情况似乎不是这样。据我所知,这是两件事之一:
你试图编辑一本不存在的书。在决定呈现窗体之前,对其执行.nil?
检查,并呈现错误消息(或重定向)
您的路由已中断,edit
操作未呈现edit
视图。情况很可能并非如此
编辑:
使用show
的模板更新后,这似乎是您的问题:
我发现这有两个问题(尽管我需要查看rake路由的输出来验证)。首先,您需要将一个参数传递给编辑路径(如果没有它们,您的参数将从何而来?)。其次,默认的路径是edit\u book\u path
试试这个:
假设您的数据库中有图书ID 14,如果您使用类似于资源:图书
(文档)的内容创建图书,则应该能够导航到localhost:3000/books/14/edit
。如果这不起作用,则可能是您的路线定义不正确,或者数据库中不存在ID为14的图书
在“显示”视图中,将链接更改为
行:
<%= link_to 'Edit', edit_book_path(book), class: "btn btn-primary" %>
因此有两个变化:
同样,假设book是Restful资源,当您运行rake\u routes
时,您应该看到要编辑的路径是edit\u book\u path
您需要将路径传递给book
实例,以便Rails知道您希望编辑哪个对象
我找到了正确的语法
希望这有帮助。您要编辑的书可能不在您的数据库中。您的URL中的图书ID是什么?你确定你的数据库中有这本书吗?您可以通过Rails控制台确认。在添加编辑功能之前,我的数据库中确实有书籍。添加编辑功能后,我删除了这些书,然后添加了一本。(这将是一本ID为14的书。它是数据库中唯一的一本。我可以在我的显示页面上单击它的编辑按钮,当我看到死亡的红色页面时!即使我刚刚添加了一本新书,我也会收到以下错误消息:当我单击它的编辑按钮时,找不到ID为的书。感到沮丧,但其中的一部分痛苦是f学习!如果您使用rake routes
查看您的路线,您应该有一个类似edit/:id
的路线。因此,如果数据库中存在id为14的图书,您应该能够导航到www.example.com/edit/14
。另外,您是否发布了包含编辑图书按钮的show view?如果没有,请作为问题可能存在。我已经添加了上面的显示页面。当我前往时,我收到一条消息,说没有路线匹配。这行可能在我的显示页面中出错了吗:不确定这是否正确,但确实是这样的:并且仍然会出现相同的错误。我同意我不认为选项2是问题所在。我认为您已经了解其中的一些内容。我知道这本书在数据库中但是它只是没有把它拉上来。谢谢你的帮助!在调用form_for
之前检查@book
是否为nil
-如果是,甚至不要调用form_for
。相反,显示某种错误。我通常会在控制器内检查它是否为nil,并重定向到不同的页面y、 仍然收到错误消息无法找到id为@Book.nil的图书?
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>
<% 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'
#@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
<%= link_to 'Edit', edit_book_path(book), class: "btn btn-primary" %>