質問と回答を作成するための新しいフォームがあります。これは私のフォームです:
<%= simple_form_for [@question_type, @question], url: path, defaults: { error: false } do |question_form| %>
<%= render 'shared/error_messages', object: question_form.object %>
<div class="question_fields well">
<%= question_form.input :content, input_html: { rows: 4, class: 'span6' } %>
<%= question_form.input :mark, input_html: { class: 'span1' } %>
<%= question_form.association :topic %>
</div>
<%= question_form.simple_fields_for :answers do |answer_form| %>
<%= render 'answer', f: answer_form %>
<% end %>
<%= question_form.button :submit, class: "new_resource" %>
<% end %>
質問には、コンテンツ、マーク、トピックの3つのフィールドがあります。
これは、create
質問コントローラーでの私のアクションです。
def create
@question = Question.new(params[:question])
@question.question_type_id = params[:question_type_id]
@question.user_id = current_user.id
if @question.save
flash[:success] = "Successfully created question."
redirect_to new_question_type_question_path
else
render 'new'
end
end
私のルート:
resources :question_types, only: [:index] do
resources :questions
end
これで、ユーザーが送信して質問を作成した後、新しいフォームが再び表示されますが、topic
選択すると、保存されたばかりの質問のトピックが表示されます。どうやってやるの?