如何将数组从psql数据库转换为字符串列表?

时间:2015-06-06 14:33:45

标签: ruby-on-rails ruby psql formtastic

我为最终项目构建了一个测验应用程序,并且它将在下周到期,我一直在努力解决一个问题,试图将数组转换为字符串列表。

问卷调查模型:

class Questionnaire < ActiveRecord::Base

  belongs_to :categories

end

Chose_answer.html.erb

<h1>Congrats You Hit The Choices Page!</h1>

<%= semantic_form_for @questions.choices do |c| %>

  <%= c.inputs do %>

    <%= c.input :choices, :as => :check_boxes , :collection => 
   [@questions.choices].map(&:inspect).join(', ') %>

  <% end %>

<% end %>

问卷调查员:

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])
    ###This finds all the questions from the question table by their category_id. Whenever I select a category, it matches the question related to the category

    render :show
    ###render :show Renders Html page
  end

  def choose_answer
    # binding.pry
    @questions = Questionnaire.find(params[:id])
    #params[:id] = /:id  = /1


    render :choose_answer
  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 virgn'", "1981

The Buggles 'Video Killed The Radio Star'"], correct_answer:"1981 The 

Buggles 'Video Killed The Radio Star' ", category_id:1})

@ question.choices返回

["1982 Michael Jackson 'Bille Jean'", "1984   

Madonna 'Like a virgin'", "1981 The Buggles 'Video Killed The Radio Star'"], 

我想转换&#34;选择&#34;进入清单。我想用formtastic将它们变成多项选择我应该怎么做?我需要有人帮忙解答这个问题,因为我真的想按时完成我的项目并让它工作。

1 个答案:

答案 0 :(得分:0)

查看文档here。您可以将数组作为集合传递:

<%= c.input :choices, :as => :check_boxes , :collection => @questions.choices %>