我一直在努力想出一个数据库调用,从问卷表中的“选择”栏给我所有的信息。我能够迭代并列出自己的选择,但是它们有自己的索引值。
代码@choices = Questionnaire.select(:choices).all[0]只给了我第一个
我希望数据库调用是@choices = Questionnaire.select(:choices).all[i],允许我访问所有这些调用。我试过我能想到的循环。如果外面有人有空,请帮帮我。我在这件事上挣扎了一整天。谢谢!
问卷主计长
class QuestionnairesController < ApplicationController
def index
@questions = Questionnaire.find(params[:category_id])
#params[:category_id]= <%=category.id%>
@category = Category.find(params[:category_id])
@videos = VideoClue.find(params[:category_id])
render :show
###render :show Renders Html page
end
def choose_answer
@questions = Questionnaire.find(params[:id])
@choices = Questionnaire.select(:choices).all
render :choose_answer
end
end结束
Choose_answer.html
<h1>Congrats You Hit The Choices Page!</h1>
<%= semantic_form_for @questions.choices do |c| %>
<%= c.inputs do |e| %>
<%= c.input :answer, :as => :check_boxes , :collection => @choices%>
<% end %>
<% end %>种子
Questionnaire.create({question: "In that year did MTV (Music Television) premiere and what was the first music video the channel aired?",
choices:'1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin, 1981 The Buggles Video Killed The Radio Star', correct_answer:"1981 The Buggles 'Video Killed The Radio Star' ", category_id:1})
Questionnaire.create({question: "This sitcom featured four girls living under one roof. They attended the same boarding school, ran a shop together and reside in a town called Peekskill." , choices:'Designing Women, The Facts of Life, Girlfriends', correct_answer:'The Facts of Life', category_id: 2})
Questionnaire.create({question: "This martial arts film premiere in 1985 which featured a young man who studies Bruce Lee's techniques while on the search for his master. This was set in New York City." , choices:'The Last Dragon, The Karate Kid, Big Trouble in Little China', correct_answer:'The Last Dragon', category_id: 3})
Questionnaire.create({question:"This game launched in 1991 on Sega Genesis which the player's mission is to collect as many golden rings as possible", choices:'Battletoads, Sonic The Hedgehog, Jewel Master', correct_answer: "Sonic The Hedgehog", category_id:4})Questionnaire.select(“选择”).all返回
[<Questionnaire:0x007fbc2c9fa728
id: nil,
choices: "1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin, 1981 The Buggles Video Killed The Radio Star">,
<Questionnaire:0x007fbc2c9fa138 id: nil, choices: "Designing Women, The Facts of Life, Girlfriends">,
<Questionnaire:0x007fbc2ca01dc0 id: nil, choices: "The Last Dragon, The Karate Kid, Big Trouble in Little China">,
<Questionnaire:0x007fbc2ca00f88 id: nil, choices: "Battletoads, Sonic The Hedgehog, Jewel Master">]发布于 2015-06-07 19:31:19
choices: '1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin,
1981 The Buggles Video Killed The Radio Star'首先要考虑的是,为什么要将所有可能的选择存储为单个字符串?你怎么知道第一个选项的文本在哪里结束,第二个选项从哪里开始?
因此,您的第一步应该是将包含字符串的单个选项划分为,ehm,选项数组。这可以通过
string到array类型的转换);这样做之后,您应该能够在视图(或rails控制台)中迭代choices。此步骤根据您先前选择的选项而有所不同。
https://stackoverflow.com/questions/30697170
复制相似问题