2014-02-28 4 views
0

안내 다음,하지만 난 (내가 기록을 전화 했어) 내 게시물 내가 할연결할 수 없습니다 모델이 제대로 레일 내가 레일 가이드를 따라하고

Rails Guide

에 댓글을 추가 어디 갇히지 해요 그들이 말하는, 내 서버 출력이 같이

@record = Record.find(params[:record_id]) 
    @comment = @record.comments.create(params[:comment].permit(:body)) 
    redirect_to record_path(@record) 
,369 :

Started POST "/records/2/comments" for 127.0.0.1 at 2014-02-28 01:28:27 +0100 
Processing by CommentsController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XC40jP5Sh6Chr3uXGoSny04n6KJI6+o8LR+txA6cE1o=", "comment"=> {"body"=>"aaa"}, "commit"=>"Create Comment", "record_id"=>"2"} 
Record Load (0.2ms) SELECT `records`.* FROM `records` WHERE `records`.`id` = 2 LIMIT 1 
(0.1ms) BEGIN 
SQL (0.4ms) INSERT INTO `records` (`created_at`, `updated_at`) VALUES ('2014-02-28 00:28:27', '2014-02-28 00:28:27') 
(6.8ms) COMMIT 
Redirected to http://localhost:3000/records/2 
Completed 302 Found in 24ms (ActiveRecord: 7.5ms) 

지금, 나는 ... 내 컨트롤러는 내가 매우 밀접하게 가이드를 따라 한 생각

내가 뭘 잘못하고 있니? 감사!

업데이트 :

UPDATE BTREE : 사용 "index_comments_on_record_id"여기 내 기록에 대한 스키마 및

create_table "comments", force: true do |t| 
t.text  "body" 
t.integer "record_id" 
t.datetime "created_at" 
t.datetime "updated_at" 
end 

create_table "records", force: true do |t| 
t.string "name" 
t.string "surname" 
t.integer "weight" 
t.integer "desired_weight" 
t.integer "height" 
t.date  "dob" 
t.string "city" 
t.string "condition" 
t.string "allergy" 
t.datetime "created_at" 
t.datetime "updated_at" 
t.string "avatar_file_name" 
t.string "avatar_content_type" 
t.integer "avatar_file_size" 
t.datetime "avatar_updated_at" 
end 

add_index 등 "의견"을 언급, [ "RECORD_ID"], 이름에서 촬영 여기에, 가이드 레일은 내가 permit가 배열을 받아 믿고, 또한 당신이 require 문을 사용해야 내 컨트롤러

@record = Record.find(params[:record_id]) 
@comment = @record.comments.create!(params[:comment].permit(:body)) 
redirect_to record_path(@record) 
+0

레코드 및 설명의 스키마를 공유 할 수 있습니까? –

+0

스키마가 괜찮아 보입니다. CommentsController의 'create'액션을 공유 할 수 있습니까? –

+0

표시된 코드는'CommentsController'' create' 액션입니까? 컨트롤러 이름이 아닌'my controller' 만 언급하면됩니다. 나는 당신이 공유 한 로그마다 데이터베이스에'record'가 생성되고'comment'를 공유 한 코드에서'record'가 생성되지 않았기 때문에 확인하고 있습니다. 불일치가 보이니? –

답변

-1

입니다.

@comment = @record.comments.create(params[:comment].permit(:body)) 

로 :

@comment = @record.comments.create(params.require(:comment).permit([:body])) 

이 같은 permited 속성을 충당하기 위해 개인 방법을 만들기 위해 그에게 좋은 아이디어를 참고 : 그래서 변경하려고

class MyController 

    def create 
    ... 
    @comment = @record.comments.create(comment_params) 
    ... 
    end 

    private 

    def comment_params 
     params.require(:comment).permit([:body]) 
    end 

end 
+0

고마워요. 여전히 문제가 있습니다 ... – alemur

+0

답을 업데이트했습니다. 변경 후 (일반적으로 필요하지 않지만 대개는 필요하지 않음) 서버를 다시 시작하십시오. – Benj

+0

불행히도 저는 여전히 동일한 문제를 겪고 있습니다. 아무 것도 쓰지 않는 것 같아요. – alemur

0

당신이 테스트를 해봤 코드

@record = Record.find(params[:record_id]) 
@comment = @record.comments.create(params[:comment].permit(:body)) 

레일 콘솔? 당신이

create!() 

대신

create() 

으로 시도하는 경우가 어떤 예외가 발생하는 경우에도 그럼 당신은 확인할 수 있습니다; 모델에 대한 유효성 검사 또는 지정되지 않은 속성 때문일 수 있습니다.

+0

코멘트 ID를 저장하지 않는 것 같아요. # alemur

+0

동의. RailsGuides에서 생성시 모든 검증이 통과되면 연관된 객체가 저장됩니다. 이 경우 주석. –

관련 문제