我的表单创建了一个问题和答案。对于真和假,我目前有一个答案是提交的,这是正确的答案。我正试图找出如何用相同的属性做出另一个答案,除非有相反的值,无论是真还是假。
因为答案是用问题创造出来的,所以我不知道该怎么做。我在想控制器有类似的东西,@question.answers.build(问题: params:content),但我迷路了。是否有@question.answers.build.where(内容:(@question.content.opposite))或其他什么?接受任何帮助
控制员:
def new_tf
@question = Question.new
@question.answers.build
end
def create
@question = Question.new(question_params)
respond_to do |format|
if @question.save
format.html { redirect_to @question, notice: 'Question was successfully created.' }
format.json { render action: 'show', status: :created, location: @question }
else
format.html { render action: 'new' }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
def question_params
params.require(:question).permit(:content, :question_type, :category, :product_id, :active, :user_id, answers_attributes: [ :content, :correct, :question_id ] ).
merge user_id: current_user.id
end 表格:
<h1>New True/False Question</h1>
<%= link_to 'Back', questions_path %>
<%= form_for @question, url: new_tf_question_path(@question) do |f| %>
<%= render 'shared/error_questions' %>
<%= f.label :content, "Question" %><br>
<%= f.text_field :content, class: "input-lg" %>
<%= f.label :category %><br>
<%= f.select :category, [ ["IP Voice Telephony", "ip_voice"], ["IP Video Surveillance", "ip_video_surveillance"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"] ], {prompt: "Select Category"}, class: "input-lg" %>
<%= f.label :product_id %><br>
<%= f.collection_select :product_id, Product.all, :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg"} %>
<%= f.label :active %><br>
<%= f.check_box :active %>
<%= f.fields_for :answers do |builder| %>
<%= render 'tf_answers', :f => builder %>
<% end %>
<%= f.select :question_type, [["True False", "TF"]], {class: "form-control input-lg"}, style: "visibility: hidden" %>
<%= f.submit "Create Question", class: "btn btn-lg btn-primary", style: "margin-top: 45px;" %>
<% end %>The _tf_answers.erb.rb
<%= f.check_box :correct, {checked: true, style: "visibility: hidden"} %>
<%= f.label :content, "Answer" %>
<%= f.text_field :content, :value => 'True', :readonly => true, :class => "input-lg", :id => "answer" %>
<%= button_tag "Toggle True/False", :id => "toggle", :class => "btn btn-small btn-inverse", :type => "button" %>
<script>
function checkAnswer() {
var answer = $('#answer').val();
if ('False' == answer) {
$("#answer").val('True');
} else {
$("#answer").val('False');
}
}
$(document).ready(function(){
$('#toggle').click(function () {
checkAnswer();
});
});
</script>发布于 2014-04-24 20:12:28
最后,在保存了第一个答案之后,我创建了一个新的答案。
if @question.save
@answer = Answer.find_by_question_id(@question.id)
if @answer.content == "True"
Answer.create(content: "False", question_id: @answer.question_id, correct: false)
end
if @answer.content == "False"
Answer.create(content: "True", question_id: @answer.question_id, correct: false)
end塔-达!
发布于 2014-04-24 20:29:53
为什么你的模型不能自动创建两个答案?扩展模型中的save方法,您将不必在控制器中使用此逻辑。不管怎么说,它很可能属于那里。
https://stackoverflow.com/questions/23278311
复制相似问题