2015-06-06 38 views
1

我正在爲最終的項目構建一個測驗應用程序,並且它在下週到期,我一直在努力嘗試將數組轉換爲字符串列表。如何將數組從psql數據庫轉換爲字符串列表?

問卷型號:

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'"], 

我想 「選擇」 轉換成一個列表。我想用formtastic把它們變成多項選擇我該怎麼做?請我需要一個人來幫助解答這個問題,因爲我真的想按時完成我的項目並讓它工作。

+1

你的公會應該是'belongs_to的:category' –

+0

我的數據庫表的名字叫做類別。爲什麼它應該是類別? –

+0

belongs_to協會應該總是單數化而不是複數化 –

回答

0

檢查文檔here。你可以傳遞一個數組作爲集合:

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

+0

沒有工作。它返回了一個未定義的'map'錯誤 –

+0

'@ question.choices'返回什麼?你可以檢查樣本數據:<%= c.input:choices,:as =>:check_boxes,:collection => [「1982 Michael Jackson'Bille Jean'','1984 Madonna'Like a virgin'」,「1981 The Buggles'Video Killed The Radio Star'「]%>' – AbM

+0

它返回數組本身。 –

相關問題