I'm trying to turn my choices column from my Questionnaire table into separate strings as multiple choice.
Questionnaire Controller:
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
end
Questionnaire Model:
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%>
Questionnaire table seed:
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 returns
["1982 Michael Jackson 'Bille Jean'", "1984
Madonna 'Like a virgn'", "1981 The Buggles 'Video Killed The Radio Star'"],
How do I separate the strings from the choices column array and use formtastic to turn the strings into a mutiple choice format?
<%= c.input :choices, :as => :check_boxes , :collection => @questions.choices %>
应该在这里执行您想要的操作,如果它已经是数组IIRC,则无需修改它,除非我误会了情况。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.