Sword Art

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in What is wrong in my script?

Hi dear community,
I created a survey app which has questions and answers as radio buttons. So firstly i asked on stackoverflow. A good guy helped me but there is some issue in form. This my survey_controller:

class SurveysController < ApplicationController
  before_action :authenticate_user!
  before_action :set_survey, only: [:show, :edit, :update, :destroy]

  # GET /surveys
  # GET /surveys.json
  def index
    @surveys = Survey.all
  end

  # GET /surveys/1
  # GET /surveys/1.json
  def show
    #set_survey


  end

  # GET /surveys/new
   def new
    @survey = Survey.new

    respond_to do |format|
      format.html # new.html.erb
      format.json 
      end 
end

  # GET /surveys/1/edit
  def edit
  end

  # POST /surveys
  # POST /surveys.json
  def create
    @survey = current_user.surveys.new(survey_params)

    respond_to do |format|
      if @survey.save
        format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
        format.json { render :show, status: :created, location: @survey }
      else
        format.html { render :new }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /surveys/1
  # PATCH/PUT /surveys/1.json
  def update
    respond_to do |format|
      if @survey.update(survey_params)
        format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
        format.json { render :show, status: :ok, location: @survey }
      else
        format.html { render :edit }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /surveys/1
  # DELETE /surveys/1.json
  def destroy
    @survey.destroy
    respond_to do |format|
      format.html { redirect_to surveys_url, notice: 'Survey was successfully destroyed.' }
      format.json { head :no_content }
    end
  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

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_survey
      @survey = Survey.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def survey_params
      params.require(:survey).permit(:id,:name, :user_id, questions_attributes:[:id, :title, :qtype, :survey_id, options_attributes:[:id, :otext, :question_id]])
    end

def response_params
  params.require(:response).permit(answers_attributes: [:question_id, :choice_id])  
end
end

and that is my response form:

= 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'

finally my routes:

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

It is givin error belov when i try open the response form:

create_response.html.haml
undefined method `question' for nil:NilClass

    - q = ff.object.question

This is the repository link if you need check: repolink

Thanks.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.