0

I'm new to Rails and I'm building a quizz application. I have a has_many and belongs_to association set up between two models: Level and Question.

#models/level.rb
class Level < ActiveRecord::Base
    has_many :questions
end

#models/question.rb
class Question < ActiveRecord::Base
    belongs_to :level
    attr_accessible :level_id

end

I have an action 'startlevel' in my LevelController that simply lists all the levels.

class LevelsController < ApplicationController

  def startlevel
    @levels = Level.all
  end

and the view with links to go to the first question of the level. I want to add the id of the level as a parameter in the link. I noticed the 1 in the url of the view. I have no idea why how it came there and if it is part of my problem.

#controller/levels/startlevel/1
<h2>which level would you like to play</h2>
  <table>
    <tr>
      <th>level</th>
    </tr>
    <% @levels.each do |level| %>
    <tr>
      <td> level <%=level.number %></td>
      <td><%= link_to '<> play this level', :controller => "questions", :action =>    "answer",:level_id=> level.id%></td>
    </tr>
    <% end %>
</table>

When I follow the the link, I want to go to the first question with a level_id that matches the id parameter in the link_to, so i tried to do this:

class QuestionsController < ApplicationController

  def answer
    @question = Question.find_by_level_id(params[:level_id])
  end

with this view

<p>
    <b>Question:</b>
    <%=h @question.word %>
</p>
<p>
    <b>1:</b>
    <%=h @question.ans1 %>
</p>
<p>
    <b>2:</b>
    <%=h @question.ans2 %>
</p>
<p>
    <b>3:</b>
    <%=h @question.ans3 %>
</p>
<p>
    <b>4:</b>
    <%=h @question.ans4 %>
</p>
    <%= form_tag(:action => "check", :id => @question.id) do %>
<p>
    <b>the correct answer is number: </b>
    <%=text_field :ans, params[:ans]%>
</p>
<p><%= submit_tag("check")%></P>
    <% end %>

unfortunately, whatever i tried, all i got for the last couple of hours was: undefined method `word' for nil:NilClass (word is an attribute of a question)

I want to cry. what am I doing wrong?

p.s. my idea is to add a link_to_unless in the 'answer'view which goes to the next question of the same level unless the next one is nil, so i think i need to group the questions with the same reference key somehow?

shann
  • 21
  • 2
  • To try to isolate the error. Do you know if your action `answer` is being called? To check that, you could hard-code you `answer` action to something like: `@question = Question.find_by_level_id(1)` – roshiro Apr 21 '14 at 18:55
  • Have you checked your routes? Could you post your routes.rb? – Ruby Racer Apr 21 '14 at 21:51

1 Answers1

0

It works right now, however i'm not sure if this is the prettiest solution. Views/levels/play is empty since it only redirects to the first question of the level.

class LevelsController < 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: 'questions', action: 'answer', id: @question.id
  end

#views/levels/startlevel

 <h2>Which level would you like to play</h2>
<table>
    <tr>
        <th>level</th>
    </tr>
    <% @levels.each do |level| %>
    <tr>
        <td> level <%=level.number %></td>
    <td>
    <%= link_to '<> Play this level', :action => "play", :id=>level.id %></td>
    </tr>
<% end %>
</table>

QuestionsController

class QuestionsController < ApplicationController
   def answer
      @question= Question.find(params[:id])
  end

edit: Routes:

quizz::Application.routes.draw do
  resources :questions
  resources :levels

  get "home/index"
  root :to => 'home#index'

  match ':controller/:action/:id', via: [:get, :post]
  match ':controller/:action/:id.:format', via: [:get, :post]
shann
  • 21
  • 2