2013-06-13 33 views
0

我花了最近三個小時試圖找到一個解決方案,並檢查了一切,我可以在這裏找到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) 

希望有人有一個想法。請?

回答

1

這應該這樣做:

def create 
    Blog.create(params[:blog]) 
    redirect_to action: 'index' 
end 

讓我知道,如果它不是。我會嘗試修改我的答案,並在此處粘貼完整的錯誤跟蹤以防萬一不起作用。控制檯登錄過,因爲你在新的動作初始化@blog_entry您可能需要創建博客與Blog.create(params[:blog_entry])

編輯

從控制檯"post"=>{"title"=>"first entry 5", "content"=>"new entry"}

意味着你需要創建一個博客條目與:Blog.create(params[:post])

+0

不,不幸的是,這也不起作用。我沒有收到任何錯誤,但是它創建了一個記錄,其中的字段值爲零。 –

+0

我也曾嘗試使用Blog.create(params {:blog_entry])具有相同的零結果。 –

+0

請按控制檯日誌,在你點按提交按鈕 – rmagnum2002

相關問題