我花了最近三個小時試圖找到一個解決方案,並檢查了一切,我可以在這裏找到StackOverflow,但沒有去。BlogsController中的NoMethodError#create
我特別收到此錯誤:"undefined method
[]」爲無:NilClass「`
和應用程序跟蹤給了我這樣的:app/controllers/blogs_controller.rb:7:in
create'`
我認爲這是與如何我傳遞的PARAMS,但我無法弄清楚什麼是錯這裏是我的控制器代碼:
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
這是我的new.html.erb文件:
<%= form_for @blog_entry, as: :post, url: {action: "create"} do |f| %>
Title: </br>
<%= f.text_field :title %> <br />
Content: <br />
<%= f.text_area :content %> <br />
<%= f.submit "Publish", class: "btn btn-primary" %> <br />
<% end %>
下面是相關的日誌文件:
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)
希望有人有一個想法。請?
不,不幸的是,這也不起作用。我沒有收到任何錯誤,但是它創建了一個記錄,其中的字段值爲零。 –
我也曾嘗試使用Blog.create(params {:blog_entry])具有相同的零結果。 –
請按控制檯日誌,在你點按提交按鈕 – rmagnum2002