Rails 4:接收以下错误:形式中的第一个参数不能包含nil或为空 [英] 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
#注意@books是如何复数的。
@books = Book.all
@authors = Author.all
#@ books = Book.where(id:params [:id])
end
#Create方法将保存新条目
def create
@book = Book.new(book_params)
@authors = Author.all
如果@book .save
flash [:success] =本书添加到Databse!
redirect_to @book
else
render'new'
end
end
private
#注意这个方法将进入上面的create方法。
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
/ pre>
validates:name,presence:true
validates:name,uniqueness:true
has_many:books
end
编辑页面:
< h1>图书条目< / h2>
< div class =row>
< div class =col-md-6 col-md-offset-3>
<%= form_for(@book)do | f | %> ** ERROR SEEMS BE 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 =&' select a author'},
class:form-control)%>< br />
< / div>
<%= f.submit'保存更改',类: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)%>
禁止保存此条目:< / h2>
< ul>
<%@ book.errors.full_messages.each do | msg | %>
< li><%= msg%>< / li>
<%end%>
< / ul>
< / div>
< / div>
<%end%>
显示页面:
< div class =move>
< h1>显示图书标题:< / 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_path,class:btn btn-primary%&
< / div>
< / div>
< / div>
<%end%>
这是我的日志告诉我什么是错的:
开始GET/编辑为:: 1在2015-08-14 16:49:17 - 0400
由BooksController处理#以HTML格式编辑
在布局/应用程序中渲染的图书/ edit.html.erb(2.2ms)
已完成500内部服务器错误9ms(ActiveRecord:0.0ms)
ActionView :: Template :: Error(form中的第一个参数不能包含nil或为空):
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:在`_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)
#行,
没有错误页面,但创建一个新条目。
#@ book = Book.new
#@ authors = Author.all
任何帮助将不胜感激!谢谢你的时间!!!
解决方案这个错误意味着
form_for
是一个
nil
值(在本例中为@book
)。大多数时候,我已经看到这一点,它通常做错误的控制器操作,但这不是在这里的情况。从我可以告诉,它是两件事之一:
您正在尝试编辑不存在的书。在决定呈现表单之前,请先执行
.nil?
,然后显示错误讯息(或重新导向)。
您的路线已损坏,
编辑
操作无法呈现编辑
视图。这很可能不是的情况。
编辑:
使用您的
show
模板更新后,这看起来像您的问题:
<%= link_toEdit,edit_path,class:btn btn-primary%>
我看到两个问题(虽然我需要看到
rake routes
的输出验证)。首先,你需要传递一个参数到编辑路径(没有它们,你的params从哪里来?)。其次,默认路由为edit_book_path
。
尝试:
<%= link_to编辑,edit_book_path(书),类别:btn btn-primary%>
I am receiving the following error on a project of mine: First argument in form cannot contain nil or be empty. I am trying to create an edit page for my code. Fairly new with Rails and trying to learn without scaffolding.
Controller:
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
Model Page: (For Book)
class Book < ActiveRecord::Base validates :title, :pub_date, :publisher, presence: true validates :title, uniqueness: true belongs_to :author end
Model Page: (For Author)
class Author < ActiveRecord::Base validates :name, presence: true validates :name, uniqueness: true has_many :books end
Edit page:
<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>
Render form page (_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 %>
SHOW PAGE:
<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 %>
Here is my Log telling me what is wrong:
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'
I will say that I have deleted the first 14 books from my data base and so the first book start on ID 14. Not sure if that matters.
Finally, I have tried adding all of these different instance variables to my controller in the edit method:
#@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
Any Help will be appreciated! Thank you for your time!!!
解决方案This error means that the first argument to
form_for
is anil
value (in this case@book
). Most of the times I've seen this, it's generally do to malformed controller actions, but that doesn't look to be the case here. From what I can tell, it's one of two things:
You're trying to edit a Book that doesn't exist. Do a
.nil?
check on it before deciding to render the form, and render an error message (or redirect) instead.Your routes are broken, and the
edit
action is not rendering theedit
view. This is most likely not the case.EDIT:
After updating with your template for
show
, this looks like your problem:
<%= link_to "Edit", edit_path, class: "btn btn-primary" %>
I see two problems with this (though I'll need to see the output of
rake routes
to verify). Firstly, you need to pass an argument to the edit path (without them, where would your params come from?). Secondly, the default route for this would beedit_book_path
.Try this:
<%= link_to "Edit", edit_book_path(book), class: "btn btn-primary" %>
这篇关于Rails 4:接收以下错误:形式中的第一个参数不能包含nil或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!