提问人:user3457760 提问时间:6/8/2014 最后编辑:varduser3457760 更新时间:1/6/2016 访问量:303
错误:表单中的第一个参数不能包含 nil 或为空
Error: First argument in form cannot contain nil or be empty
问:
我的form_for有一些问题,我一直收到这个错误
形式中的第一个参数不能包含 nil 或为空
这是表格
<%= form_for @blog do |f| %>
<%= f.text_area :blogpost %>
<%= f.submit 'Add Blog'%>
<% end %>
这是我的控制器
class BlogsController < ApplicationController
def index
@blogs = Blog.all #.paginate(page: params[:page], per_page: 5)
end
def create
@blog = Blog.create(blog_params)
redirect_to root_path, notice: "Created a Blog"
end
def update
@blog = Blog.create(blog_params)
redirect_to root_path
end
def show
@blog = Blog.find(params[:id])
end
def new
@blog = Blog.new
end
def edit
@blog = Blog.find(params[:id])
end
def destroy
@blog = Blog.find(params[:id])
@blog.destroy
redirect_to root_path
end
private
def blog_params
params.require(:blog).permit(:blogpost)
end
end
我该如何解决这个问题?
答:
1赞
Pavan
6/8/2014
#1
由于它属于 index.html.erb
,因此您应该将此行添加到方法中。@blog = Blog.new
index
def index
@blogs = Blog.all
@blog = Blog.new
end
而且您的更新
方法也是错误的。应该这样,以避免将来出现错误。
def update
@blog = Blog.update(blog_params)
redirect_to root_path
end
评论
0赞
user3457760
6/8/2014
就这么简单??非常感谢!
评论
form
view file