anastasiaalt / quiz

Anastasia's Ruby on Rails Project
1 stars 0 forks source link

Accessing Nested Resources in Forms #4

Closed anastasiaalt closed 8 years ago

anastasiaalt commented 8 years ago

This question is similar to the one I just posted but for forms. I want to make it so that when you are creating a new quiz, you can add a question to it. My research suggested using fields_for could be the best bet but not really sure.

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

The examples I found were more so for check boxes (which I know I will need to change the answer choice options from their default of false to true in order to indicate what is a correct answer eventually) but not for displaying all the options.

Not much I can find (probably due to the way I am describing this issue in the search as accessing nested resources in a ruby on rails form). One article I found but not super helpful: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

I am getting: undefined method `ask'

for this code

  <div>
    <%= fields_for @quiz.questions do |question| %>
    <%= question.ask %>
    <% end %>
  </div>
  <div>

Again, I get the concept well I am just really struggling with syntax :(

Avizacherman commented 8 years ago

You're going to need a nested loop here as well I believe. The block scope you're in for fields_for isn't on the @quiz, but rather on the actual form helper. So you should be creating a form helper block (fields_for something do |f|) and then inside you can create a loop for that something (something.each do |thing|). Let me know if that makes sense.