首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用第一个答案的相反属性建立答案

用第一个答案的相反属性建立答案
EN

Stack Overflow用户
提问于 2014-04-24 19:40:29
回答 2查看 77关注 0票数 0

我的表单创建了一个问题和答案。对于真和假,我目前有一个答案是提交的,这是正确的答案。我正试图找出如何用相同的属性做出另一个答案,除非有相反的值,无论是真还是假。

因为答案是用问题创造出来的,所以我不知道该怎么做。我在想控制器有类似的东西,@question.answers.build(问题: params:content),但我迷路了。是否有@question.answers.build.where(内容:(@question.content.opposite))或其他什么?接受任何帮助

控制员:

代码语言:javascript
运行
复制
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  

表格:

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
<%= 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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-24 20:12:28

最后,在保存了第一个答案之后,我创建了一个新的答案。

代码语言:javascript
运行
复制
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

塔-达!

票数 0
EN

Stack Overflow用户

发布于 2014-04-24 20:29:53

为什么你的模型不能自动创建两个答案?扩展模型中的save方法,您将不必在控制器中使用此逻辑。不管怎么说,它很可能属于那里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23278311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档