2013-06-13 9 views
0

나는이 문제에 대한 해결책을 찾기 위해 지난 3 시간을 보냈고 StackOverflow에서 찾을 수있는 모든 것을 확인했지만 아무 것도하지 않았다. "undefined method [] '무기 호에 대한 :NoMethodError in BlogsController # create

내가 특별히이 오류가 무엇입니까 NilClass "`

을 그리고 응용 프로그램 추적은 저에게이 제공 : 내가 어떻게 함께 할 수있는 뭔가가 생각

app/controllers/blogs_controller.rb:7:in create'`을 나는 PARAMS을 전달하고 그러나 나는 무엇이 잘못되었는지 알아낼 수 없습니다 여기 내 컨트롤러 코드입니다 :.

,691,363 :
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 파일입니다

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) 

희망 누군가가 아이디어가있다 : (210)

<%= 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 %> 

여기에 관련 로그 파일입니다. 부디?

답변

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

아니요, 불행히도 그 중 하나가 작동하지 않습니다. 어떤 오류도 발생하지 않지만 필드에 대해 nil 값을 갖는 레코드를 생성합니다. –

+0

나는 또한 같은 nil 결과를 가진 Blog.create (params {: blog_entry])를 사용해 보았습니다. –

+0

콘솔 로그는 제출 버튼을 누르십시오. – rmagnum2002