首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据库调用问题

问数据库调用问题
EN

Stack Overflow用户
提问于 2015-06-07 18:43:22
回答 1查看 37关注 0票数 0

我一直在努力想出一个数据库调用,从问卷表中的“选择”栏给我所有的信息。我能够迭代并列出自己的选择,但是它们有自己的索引值。

代码@choices = Questionnaire.select(:choices).all[0]只给了我第一个

我希望数据库调用是@choices = Questionnaire.select(:choices).all[i],允许我访问所有这些调用。我试过我能想到的循环。如果外面有人有空,请帮帮我。我在这件事上挣扎了一整天。谢谢!

问卷主计长

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

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

种子

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

代码语言:javascript
复制
     [<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">]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-07 19:31:19

代码语言:javascript
复制
choices: '1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin,
1981 The Buggles Video Killed The Radio Star'

首先要考虑的是,为什么要将所有可能的选择存储为单个字符串?你怎么知道第一个选项的文本在哪里结束,第二个选项从哪里开始?

因此,您的第一步应该是将包含字符串的单个选项划分为,ehm,选项数组。这可以通过

  • 数据库重组(从string到array类型的转换);
  • 拆分初始字符串(但如果您的某些选择包含逗号,则情况会很糟) Questionnaire.first.choices.split(',‘') # => "1982 Michael“,”1984年麦当娜像处女一样“,”1981年Buggles视频杀死了电台明星“

这样做之后,您应该能够在视图(或rails控制台)中迭代choices。此步骤根据您先前选择的选项而有所不同。

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

https://stackoverflow.com/questions/30697170

复制
相关文章

相似问题

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