我正在爲最終的項目構建一個測驗應用程序,並且它在下週到期,我一直在努力嘗試將數組轉換爲字符串列表。如何將數組從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把它們變成多項選擇我該怎麼做?請我需要一個人來幫助解答這個問題,因爲我真的想按時完成我的項目並讓它工作。
你的公會應該是'belongs_to的:category' –
我的數據庫表的名字叫做類別。爲什麼它應該是類別? –
belongs_to協會應該總是單數化而不是複數化 –