Ask A Question

Notifications

You’re not receiving notifications from this thread.

What is wrong in my script?

Sword Art asked in Rails

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.

Reply

Your new_response action sets up answers in memory, but your create_response action is the one to save it.

However you're rendering create_response.html.haml where the error came from. Seems like you're looking at the wrong view.

Possibly fixed by doing the following:

def create_response
  @survey = Survey.find(params[:id])
  @response = @survey.build(response_params)
  @response.user = current_user
  if @response.save
      redirect_to @response
    else
      render action: :new_response
    end
end

That'll redirect to the response if it saves successfully and mimics the normal create action.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.