-1

I have a questionnaire that needs to be answered.

My model has table answers and table questions and i pass correctly but I get an error

undefined method 'each' for nil:NilClass

I just need a way to pass this array to the controller.

i think the error means that i'm passing an empty array so if you can help me with another way to write the code or edit it to be working.

my view :

           <%@answers_array = []%>
    <% @questionnaire.qquestions.each do |question| %>
        <strong><%=question.body %></strong>
        </br>
    <%= text_field_tag 'answers_array[]',""%> 
            <%end%>
 <p><%= link_to "Done",:controller => :answer_questionnaires, :action => :create, :params=>{:id => @questionnaire.id, :@answers=>@answers_array}  %></p>

my controller:

def create
  @questionnaire = Questionnaire.find(params[:id])
  number = 1
  @answers = params[:@answers]
  @answers.each do |answer| 
      AnswerQuestionnaire.create(:body => answer,:qquestion_id =>number)
      number = number + 1  
  end
end

1 Answer 1

0

I think you would be much better off creating a nested form rather than passing params around like that. There are a few good railscasts about nested forms here.

3
  • i'm trying to tell the controller that the :@answers (which is in the controller)it's value is @answers_array(in the view) in the link_to plus theres no form this is the only loop Apr 19, 2013 at 10:14
  • Ok I see now what you're trying to do. Still I would encourage you to rethink your strategy and look at those railscasts.
    – mind.blank
    Apr 19, 2013 at 10:20
  • will give it a try and will keep you posted Apr 19, 2013 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.