Hi dear community,
I created a survey app which has questions and answers as radio buttons. So firstly i asked onstackoverflow(click-here). A good guy helped me but there is some issue in form. This my survey_controller:
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
Here is my create_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'
My routes file:
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 showing error below 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: Repository - Link
Thanks for all helps.