질문을 추가 할 때이 방법을 사용합니다 (퀴즈에 할당 함). 선택 사항 (문자열 배열)을 제외한 모든 필드가 성공적으로 작성되었습니다. 문제가 무엇인지 알 수 없습니다.허용되지 않는 매개 변수
quizzes_controller.rb :
def add_question
question = Question.new(question_params)
if (current_instructor.quizzes.exists?(:id => params[:quiz_id]))
quiz = Quiz.find(params[:quiz_id])
if question.save
quiz.questions << question
render json: { success: true, data:{:question => question}, info:{} }, status: 201
else
render json: { success: false, data:{}, :info => question.errors }, status: 422
end
else
render json: { success: false, data:{}, info:"Quiz is not found"}, status: 422
end
end
private
def question_params
params.require(:question).permit(:text, :mark, :right_answer,{:choices => []})
end
questions.rb :
class Question < ActiveRecord::Base
serialize :choices,Array
belongs_to :quiz
validates :text, presence: true
validates :right_answer, presence: true
end
컬 -v -H '콘텐츠 유형 : 응용 프로그램/JSON'-H '수락 : 응용 프로그램/JSON을'- H "X-Instructor-Email : [email protected]"# -H "X-Instructor-Token : gjE7xULgNLSorQgqr689"-X POST http://localhost:3000/api/quizzes/addquestion/1 -d "
{\"질문 \ ": "당신의 이름은 무엇입니까?", \ "표시 \": \ "10 \", \ "right_answer \": \ "a \", \ "choices \": \ "[a, b, c, d] \ "}}"
터미널 : 선택 항목을 제외하고 질문이 생성되고 모든 속성이 작성됩니다. { "success": true, "data": { "question": { "id": 13, "text": " 귀하의 이름? ","표시 ": 10.0,"선택 사항 ": [],"right_answer ":"a ","quiz_id ": 1,"created_at ":"2015-02-06T14 : 07 : 15.677Z " "updated_at": "2015-02- 06T14 : 07 : 15.806Z"}}, "정보"내가 가진 서버에서 {}} %
: 허가되지 않은 매개 변수 : 선택
마이그레이션 :
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.text :text
t.float :mark
t.text :choices
t.string :right_answer
t.belongs_to :quiz, index: true
t.timestamps
end
end
end
좋아, 그래서 여기에 귀하의 응용 프로그램을 재현 : https://gist.github.com/dimanyc/0d77f4b889bc2004351e. 열 유형이 정상적으로 보입니다. @Stanislav Mekhonoshin은 해쉬에'선택 사항 '을 래핑하는 것에 대해 옳았다. 그럴 필요 없어. 'params.require (: question) .permit (: body, : mark, : choices, : right_answer)'와 같이 강력한 매개 변수를 그대로 놔두고'serialize : choices, Array'를 모델에 추가하십시오. 내 요점을 보아라. 알려줘. –
문제가 curl 명령에서 변경되지 않고 해결 될 수 있습니다. –