Rails调查回复表

时间:2018-11-10 16:54:48

标签: ruby-on-rails ruby

我有一个调查应用程序,但最后却感到困惑。 这是应用程序模型的属性:

User

- Survey
    user_id

title
- Question
    title
    survey_id
    type: multiple_choice|check_boxes|short_answer
- Option
    title
    question_id

                   (Till here is okay. I can create surveys these includes more nested forms) Issue is after created surveys. (users responses) 

-Response
    user_id
    survey_id
-Answer
    question_id
    response_id
    option_id

可以使用嵌套属性创建调查。问题出在响应方面。我在Survey show.html.erb上的响应控制器和响应表单应该如何?

下面有响应控制器的嵌套属性;

  def response_params
    params.require(:response).permit(:id, :user_id, :survey_id, answers_attributes:[:question_id, :response_id, :option_id ] )
  end

我应该告诉您,调查可以仅通过单选按钮包含多个问题(独立的单选按钮是其他问题)

这个问题让我很累。如果您能帮助我,我会很高兴。谢谢。 对于源代码:Click for source codes

更新的文件:

响应模型:

class Response < ApplicationRecord
    belongs_to :user
  belongs_to :survey
  has_many :answers, dependent: :destroy
  validates :survey, presence: true
  counter_culture :option
  accepts_nested_attributes_for :answers
end

测量控制器:

def new_response
@survey = Survey.find(params[:id])
@response = @survey.responses.build

    # now, the tricky part, you have to build the Answer objects so you can use the nested form later
    @survey.questions.each do |q|
      @response.answers.build question: q
    end
  end



def create_response
  @survey = Survey.find(params[:id])
  @response = @survey.build(response_params)
  @response.user = current_user
  @response.save
end

路线:

Rails.application.routes.draw do

  devise_for :users
    resources :surveys do 
    member do
      get :new_response
      get :create_response 
    end
        end
  root 'surveys#index'

  end

表格:

- # surveys/new_response.html.haml
- # You need to define a nested route inside survey resource to create the response
= form_for @response, url: create_response_survey_path(@survey) do |f|
  - # you can iterate over all the answers already initialized
  = f.fields_for :answers do |ff|
    - # get the question from the current answer to show the title and options and a hidden_field with the question id
    - q = ff.object.question

    = q.title
    = ff.hidden_field :question_id

    - # add the radios for each options for the question
    - q.options.each do |option|
      = label_tag do
        = ff.radio_button :option_id, option.id
        = option.title

  = f.submit 'Send'

1 个答案:

答案 0 :(得分:0)

我不会使用调查的show动作来显示创建响应的表单,我认为最好将其作为new_response动作来使其更整洁并保留{{1 }}只是为了显示实际调查(不回答)。像这样:

show

现在,您可以为回复创建表格:

class SurveysController < ApplicationController
  def new_response
    @survey = Survey.find(params[:id])
    @response = @survey.responses.build

    # now, the tricky part, you have to build the Answer objects so you can use the nested form later
    @survey.questions.each do |q|
      @response.anwers.build question: q
    end
  end

您的- # surveys/new_response.html.haml - # You need to define a nested route inside survey resource to create the response = form_for @response, url: create_response_survey_path(@survey) do |f| - # you can iterate over all the answers already initialized = f.fields_for :answers do |ff| - # get the question from the current answer to show the title and options and a hidden_field with the question id - q = ff.object.question = q.title = ff.hidden_field :question_id - # add the radios for each options for the question - q.options.each do |option| = label_tag do = ff.radio_button :choice_id, option.id = option.title = f.submit 'Send' 应该类似于:

response_params

请注意,我删除了def response_params params.require(:response).permit(answers_attributes: [:question_id, :choice_id]) end :survey_id,您不希望用户入侵您的表单,更改Survey_id或user_id并添加对其他用户所做的其他调查的响应! / p>

和您的:user_id动作:

create_response

希望有道理。