Skip to content

Instantly share code, notes, and snippets.

@fnta
Last active November 13, 2015 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnta/55241fe153e5c66d8844 to your computer and use it in GitHub Desktop.
Save fnta/55241fe153e5c66d8844 to your computer and use it in GitHub Desktop.
AnswerにQuestion.idを保存できるようにする

やること

  • QuestionのShow.html.erbにある、Answer作成リンクに「Question.id」をパラメータ追加する
  • answers_controller.rbのnewで、上記パラメータをセットする

解答作成のリンクは、今こんな感じだと思う。

<%= link_to 'create', new_answer_path %>

これを、

<%= link_to 'create', new_answer_path(question_id: @question.id) %>

みたいにすると、Answerの作成画面に遷移した際にparamsの中にquestion_idが入っているはず。 これを保存時にセットすれば、AnswerにQuestion.idを保存できます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment