associations - rails find_by: get the first item by adding the reference key as parameter -


I am new to rail and I am creating a quiz application. I have one set up between the two models: the union and affiliates have union: level and question.

  #model / levelRb class level & lt; ActiveRecord :: Base has has_many: question end # model / query. Rb class question & lt; ActiveRecord :: Base is_to: level attr_accessible: level_id end   

I have 'startlevel' in a LevelController that lists all the levels. Class Level Controller & lt; ApplicationController def startlevel @levels = Level.all end

and to go to the first question at the scene level with the link. I want to add the level ID as a parameter of the link. I saw 1 in the URL seen. I do not know how it came and why it is part of my problem.

  # controller / level / startlevel / 1 lieutenant; H2 & gt; At what level do you want to play & lt; / H2 & gt; & Lt; Table & gt; & Lt; TR & gt; & Lt; Th & gt; Level & lt; / Th & gt; & Lt; / TR & gt; & lt;% @ level.each do | Level | & Gt%; & Lt; TR & gt; & Lt; TD & gt; Level & lt;% = level.number% & gt; & Lt; / Td> & Lt; Td> & Lt;% = link_to '& lt; & Gt; Run this level ',: admin = & gt; "Question",: verb = & gt; "Answer",: level_id = & gt; Level.id% & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt;% end% & gt; & Lt; / Table & gt;   

When I follow the link, then I want to go to the first question with a level_id which matches the id parameter in link_to, so I tried to: < / p>

  Class Questions Controller & lt;   

with this view

  & lt; P & gt; AP Controller DF Answer @ Questation = Question. Found_b_love_id (param [[level_id]) & lt; P & gt; Question: & lt; / P & gt; & Lt;% = h @ Question.word% & gt; & Lt; / P & gt; & Lt; P & gt; & Lt; P & gt; 1: & lt; / P & gt; & Lt;% = h @ Question 1%> & Lt; / P & gt; & Lt; P & gt; & Lt; P & gt; 2: & lt; / P & gt; & lt;% = h @ Q. 2% & gt; & Lt; / P & gt; & Lt; P & gt; & Lt; P & gt; 3: & lt; / P & gt; & Lt;% = h @ Question 3% & gt; & Lt; / P & gt; & Lt; P & gt; & Lt; P & gt; 4: & lt; / P & gt; & Lt;% = h @ Q. 4% & gt; & Lt; / P & gt; & Lt;% = form_tag (: action = & gt; "check" ,: id = & gt; @ question.id)%> & Lt; P & gt; & Lt; B & gt; The correct answer number is: & lt; / B & gt; & lt;% = text_field: ans, params [: ans]% & gt; & Lt; / P & gt; & Lt; P & gt; & lt;% = submit_tag ("Check")%> & Lt; / P & gt; & Lt;% end% & gt; Unfortunately, whatever I tried, it was for the last few hours:  Undefined method `for the word 'zero: NilClass  (a feature of the word is a question)  

I have to cry what am I doing wrong?

My idea is to add link_to_unless to answer'view which goes to the next level to the next level to the next level, so I think that in any way the questions with the same reference key Need to be grouped?

It just works, though I'm not sure that this is the best solution to view / level / The game is empty because it only redirects the first question of the level.

  Class Level Controller & lt; ApplicationController def startlevel @levels = Level.all end def play = level = Level.find (params [: id]) @question = Question.find_by_level_id (params [: id]) redirect_to Controller: 'question', verb: 'answer' , id: @ question.id end #view / level / startlevel & lt; H2 & gt; Which level do you want to play & lt; / H2 & gt; & Lt; Table & gt; & Lt; TR & gt; & Lt; Th & gt; Level & lt; / Th & gt; & Lt; / TR & gt; & Lt;% @ level.each do | Level | & Gt%; & Lt; TR & gt; & Lt; TD & gt; Level & lt;% = level.number% & gt; & Lt; / Td> & Lt; TD & gt; & Lt;% = link_to '& lt; & Gt; Run this level ',: verb = & gt; "Play",: id = & gt; Level.id%> gt; & Lt; / Td> & Lt; / TR & gt; & Lt;% end% & gt; & Lt; / Table & gt;   

Question Controller

  Class Questions Controller & lt; ApplicationController def answer @ question = question.fund (param [: id]) expiration   

edit: route:

  quizz :: application.routes.draw Resource: Question processing: Level "Home / Index" get root: to = & gt; 'HOME # INDEX' MATCH: Controller /: Action: Through ID:,: [: Receive, Post] Match ': Controller /: Action /: ID: Format', via: [: Mill: Post]    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -