使用formtastic分裂和数组。错误:未定义的方法`map'

时间:2015-06-06 01:11:04

标签: ruby-on-rails arrays ruby psql formtastic

我试图将我的选择列从我的问卷调查表变成单独的字符串作为多项选择。

问卷调查员:

    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

问卷调查模型:

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.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 virgn'", "1981 The Buggles 'Video Killed The Radio Star'"], 

如何将字符串与选择列数组分开,并使用formtastic将字符串转换为多种选择格式?

1 个答案:

答案 0 :(得分:0)

<%= c.input :choices, :as => :check_boxes , :collection => @questions.choices %>应该在这里做你想做的事情,如果它已经是阵列IIRC,则无需修改它,除非我误解了这种情况。