formbuilder - Rails: If my Quiz model should be created on completing the quiz, how does form_for know what to refer to? -


i've been told should not create quiz object before quiz completed; user go quiz page, not complete it, , there 'unused' quiz sitting on database. can see logic of that.

i can't see how quiz supposed work without being passed @quiz object. here's quizzescontroller, which, when quiz needed, gets routed 'new' action:

class quizzescontroller < applicationcontroller         def index          end          def new             @user = current_user             @quiz = quiz.create(user_id: current_user.id)          end          def create             @results = quiz.where(user_id: current_user.id).last             redirect_to results_path         end end 

at moment, can see i'm coding actions possible. later, in 'new' action, i'll add test see if current_user has done quiz and, if so, redirect results_path.

here form partial rendered part of quizzes/new.html.erb:

<%= form_for(@quiz) |f| %>   <p>     <%= f.check_box(:answer1) %>     <%= f.check_box(:answer2) %>     <%= f.check_box(:answer3) %>     <%= f.check_box(:answer4) %>     <%= f.check_box(:answer5) %>     <%= f.check_box(:answer6) %>     <%= f.check_box(:answer7) %>     <%= f.check_box(:answer8) %>   </p>   <p>     <%= f.submit("get results!") %>   </p> <% end %> 

once again, quiz simple while figure out what's going on.

but i'd know, if @quiz object not created in 'new' action, pass form_for build form?

you can instantiate quiz object without saving database:

def new   @user = current_user   @quiz = quiz.new(user_id: current_user.id) end 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -