rails 4传递许多变量

时间:2014-05-14 17:42:57

标签: ruby-on-rails ruby-on-rails-4

我正在为我的rails应用程序创建一个调查,它基本上包含3种类型的问题,单选按钮,复选框和幻灯片范围。并且可能存在无数个问题

我已完成创建调查,现在当用户回答问题时

第一:我怎样才能得到他选择的所有答案?

第二:如何将所有这些变量传递给控制器​​submit中的new_survey方法,以便我可以用它来更新数据库,这是最好的方法还是我应该做些什么呢?

这是我的show页面

<% for @question in @new_survey.questions %>
<li><%= h @question.content %></li>
<ul>
  <% for answer in @question.answers %>
    <li><%= radio_button_tag "poll", answer.content ,true%>
      <%= h answer.content %></li>
  <% end %>
  <% for pic_model in @question.pic_models %>
    <li>
      <label>
        <input type="checkbox">
        </input>
        <img src="<%= pic_model.pic_url %>" width="100" height="100" />
      </label>
    </li>
  <% end %>
  <% for range_slider in @question.range_sliders %>
    <input type="range" min="<%= range_slider.min %>" max="<%= range_slider.max %>" value="<%= range_slider.value %>" onchange="showValue(this.value)" />
    <span id="range">0</span>
  <% end %>
  <% for rank in @question.ranks %>
    <li><%= h rank.content %></li>
  <% end %>
</ul>
   

模型:

class Question < ActiveRecord::Base
belongs_to :new_survey
has_many :ranks, dependent: :destroy
has_many :answers, dependent: :destroy
has_many :pic_models, dependent: :destroy
has_many :range_sliders, dependent: :destroy
accepts_nested_attributes_for :ranks, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :answers, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :pic_models, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :range_sliders, :reject_if => :all_blank, :allow_destroy => true
end

class Answer < ActiveRecord::Base
belongs_to :question
end

class NewSurvey < ActiveRecord::Base
  has_many :questions, dependent: :destroy
  accepts_nested_attributes_for :questions, :reject_if => :all_blank, :allow_destroy => true
end

我唯一拥有的控制器是new_survey

lass NewSurveysController < ApplicationController
before_action :set_new_survey, only: [:show, :edit, :update, :destroy]
# GET /new_surveys
# GET /new_surveys.json
def index
  @new_surveys = NewSurvey.all
end

# GET /new_surveys/1
# GET /new_surveys/1.json
def show
end

def submit
end

# GET /new_surveys/new
def new
@new_survey = NewSurvey.new
end

# GET /new_surveys/1/edit
def edit
end

# POST /new_surveys
# POST /new_surveys.json
def create
  @new_survey = NewSurvey.new(new_survey_params)
  respond_to do |format|
  if @new_survey.save
    format.html { redirect_to @new_survey, notice: 'New survey was successfully created.' }
    format.json { render action: 'show', status: :created, location: @new_survey }
  else
    format.html { render action: 'new' }
    format.json { render json: @new_survey.errors, status: :unprocessable_entity }
  end
end
end
# PATCH/PUT /new_surveys/1
# PATCH/PUT /new_surveys/1.json
def update
  respond_to do |format|
  if @new_survey.update(new_survey_params)
    format.html { redirect_to @new_survey, notice: 'New survey was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: 'edit' }
    format.json { render json: @new_survey.errors, status: :unprocessable_entity }
  end
end
end

# DELETE /new_surveys/1
# DELETE /new_surveys/1.json
def destroy
  @new_survey.destroy
  respond_to do |format|
  format.html { redirect_to new_surveys_url }
  format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_new_survey
  @new_survey = NewSurvey.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def new_survey_params
  params.require(:new_survey).permit(:name, :user_id)
end
def new_survey_params
  params.require(:new_survey).permit(:name, :user_id, questions_attributes: [:content, :_destroy, answers_attributes: [:content, :_destroy], pic_models_attributes: [:pic_url, :_destroy], range_sliders_attributes: [:max, :min, :value, :_destroy], ranks_attributes: [:content, :_destroy]]) 
end
end

1 个答案:

答案 0 :(得分:0)

要回答第二个问题,请使用views/new_surveys/_form.html.erb创建嵌套对象:

railscast应该可以帮到你。它是关于嵌套的survey, questions, and answers。如果您是railcast订阅者,则可以观看revised episode

关于你的第一个问题:how can i get all the answers he chose?。这取决于你如何嵌套你的路线。例如,您将路线嵌套为

resources :users do
  resources :answers
end

然后,您可以转到/users/:user_id/answers以获取该特定用户的所有答案。 这是app/views/answers/index.html.erb

如果这些Railscasts视频无法提供帮助,请与我们联系。