Ruby on rails BlogsController#create中的NoMethodError
我花了三个小时试图找到一个解决方案,并检查了StackOverflow上的所有内容,但没有成功 我特别得到了这个错误:Ruby on rails BlogsController#create中的NoMethodError,ruby-on-rails,controller,params,Ruby On Rails,Controller,Params,我花了三个小时试图找到一个解决方案,并检查了StackOverflow上的所有内容,但没有成功 我特别得到了这个错误:“对于nil:NilClass,未定义的方法[]”` 应用程序跟踪告诉我:app/controllers/blogs\u controller.rb:7:increate'` 我想这和我是如何传递参数有关,但我不知道是怎么回事。这是我的控制器代码: class BlogsController < ApplicationController def index @blog
“对于nil:NilClass,未定义的方法
[]”`
应用程序跟踪告诉我:app/controllers/blogs\u controller.rb:7:in
create'`
我想这和我是如何传递参数有关,但我不知道是怎么回事。这是我的控制器代码:
class BlogsController < ApplicationController
def index
@blogs = Blog.all
end
def new
@blog_entry = Blog.new
end
def create
Blog.create(title: params[:blogs][:title], content: params[:blogs][:content])
redirect_to action: 'index'
end
end
希望有人有主意。请?这应该可以:
def create
Blog.create(params[:blog])
redirect_to action: 'index'
end
如果不是,请告诉我。我将尝试修改我的答案,并请粘贴完整的错误跟踪在这里,以防它不起作用。控制台也会记录日志,当您在新操作中初始化@blog\u条目时,您可能需要使用blog.create(params[:blog\u条目])创建blog
编辑
从控制台“post”=>{“title”=>“first entry 5”,“content”=>“new entry”}
意味着您需要使用以下命令创建博客条目:blog.create(params[:post])
否,不幸的是,这也不起作用。我没有收到任何错误,但它正在为字段创建一个值为零的记录。我还尝试使用Blog.create(params{:Blog_entry])同样的结果为零。控制台记录,请在您用rails s或passenger start启动rails服务器的终端上按submit按钮的位置。这里有服务器日志,当您在应用程序上执行某些操作时,它会显示日志,以及应用程序内部发生的情况。当您提交日志时,您必须看到hing like于2013-06-13 07:46:37+0300由BlogsController处理127.0.0.1开始发布“/blogs”#创建为HTML
这只是一个示例,您使用的rails版本是什么?
Started POST "/blogs" for 127.0.0.1 at 2013-06-12 21:54:48 -0700
Processing by BlogsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"cSGH7PgbbFHSSEPV3pJ2LP6V1GvUN10RHRfUDTUXou4=", "post"=>{"title"=>"first entry 5", "content"=>"new entry"}, "commit"=>"Publish"}
[1m[35m (0.1ms)[0m begin transaction
[1m[36mSQL (0.5ms)[0m [1mINSERT INTO "blogs" ("content", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?)[0m [["content", nil], ["created_at", Thu, 13 Jun 2013 04:54:48 UTC +00:00], ["title", nil], ["updated_at", Thu, 13 Jun 2013 04:54:48 UTC +00:00]]
[1m[35m (3.2ms)[0m commit transaction
Redirected to http://localhost:3000/blogs
Completed 302 Found in 5ms (ActiveRecord: 3.7ms)
def create
Blog.create(params[:blog])
redirect_to action: 'index'
end