Ruby on rails 从Rails 4中的数组创建和设计复选框

Ruby on rails 从Rails 4中的数组创建和设计复选框,ruby-on-rails,ruby,arrays,twitter-bootstrap,checkbox,Ruby On Rails,Ruby,Arrays,Twitter Bootstrap,Checkbox,我正在努力在Rails4中创建和引导设计一个简单的调查。用户(客户机)通过录音进行了许多调查。我们打算要求用户每隔一段时间填写一份全新的调查,以便每个客户都有一份已完成调查的档案。我的模型是这样的: class User < ActiveRecord::Base has_many :recordings has_many :surveys, through: recordings ######### class Survey < ActiveRecord::Base

我正在努力在Rails4中创建和引导设计一个简单的调查。用户(客户机)通过录音进行了许多调查。我们打算要求用户每隔一段时间填写一份全新的调查,以便每个客户都有一份已完成调查的档案。我的模型是这样的:

class User < ActiveRecord::Base
  has_many :recordings
  has_many :surveys, through: recordings

#########

class Survey < ActiveRecord::Base
  has_many :recordings
  has_many :users, through: :recordings

#########

class Recording < ActiveRecord::Base
  belongs_to :user
  belongs_to :survey
class用户
可能的问题#1:我决定不需要将调查分为“调查有很多问题”和“问题有很多答案”。每个调查的问题都是静态的,因此,我的观点是,每个调查只需要标准的列条目,就好像它是任何其他形式一样

这么说来,我对调查问题做了类似的事情(一个问题的抽样调查)

班级调查
然后在控制器中:

class Survey < ApplicationController

  def new
    @survey = current_user.surveys.build
    @foods = Survey.favorite_foods
  end

  def create
    @survey = current_user.surveys.build(survey_params)
      if @survey.save
          redirect_to surveys_path, notice: "Thank you for filling out our client survey."
      else
         flash[:notice] = "Your survey has not been completed."
         render 'new'
      end
  end

  #....standard crud for has_many

  private

  def survey_params
    params.require(:survey).permit({:favorite_foods => []},....
  end
类调查[]},。。。。
结束
最后,关于这个观点:

 <%= form_for [@user, @survey] do |f| %>

 <div class="control-group">
    <div class="controls">
     <h5>What are your favorite foods?</h5>
        <div class="btn-group"><h6>
    <%  @foods.each do |food| %>
      <button class="btn" type="button" data-toggle="button"><%= check_box_tag "food_array[]", food %> <%= food %></button>

<% end %></h6><%= f.submit%>

你最喜欢的食物是什么?
问题:

我有一个功能问题和一个设计问题

  • 我当前的代码被破坏,单击复选框,然后提交表单,将表单返回给我

  • 按钮上有一个复选框,但我希望按钮本身充当复选框

  • 我看过这部《铁路史》: 但我不认为我需要在控制器中为我的案子做额外的动作。让我知道

     <%= form_for [@user, @survey] do |f| %>
    
     <div class="control-group">
        <div class="controls">
         <h5>What are your favorite foods?</h5>
            <div class="btn-group"><h6>
        <%  @foods.each do |food| %>
          <button class="btn" type="button" data-toggle="button"><%= check_box_tag "food_array[]", food %> <%= food %></button>
    
    <% end %></h6><%= f.submit%>