【问题标题】:Error: First argument in form cannot contain nil or be empty错误:表单中的第一个参数不能包含 nil 或为空
【发布时间】:2014-06-07 16:17:56
【问题描述】:

我的 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

我该如何解决这个问题?

【问题讨论】:

  • 你的form属于哪个view file
  • 我在博客视图文件夹中的 index.html.erb 中有它

标签: ruby-on-rails forms ruby-on-rails-4 controller


【解决方案1】:

由于它属于 index.html.erb,您应该将这一行 @blog = Blog.new 添加到 index 方法中。

def index
@blogs = Blog.all  
@blog = Blog.new
end

而且你的update方法也是错误的,应该是这样,以免以后出错。

def update
  @blog = Blog.update(blog_params)
  redirect_to root_path 
 end

【讨论】:

  • 就这么简单??非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多