Tim
Tim

Reputation: 3221

Rails routing error, Object being assigned to param key?

I have objects Quiz and Question.

class Question < ActiveRecord::Base
  belongs_to :quiz

  def to_param
    quesion_permalink
  end
end

class Quiz < ActiveRecord::Base
  has_many :questions

  def to_param
    permalink
  end
end

routes.rb:

match '/quizzes/:permalink/questions/:questions_permalink', to: 'questions#show', via: 'get', as: 'question'

When I put the link question_path(q) in the quiz#show page I get this error:

No route matches {:controller=>"questions", :action=>"show", :permalink=>#<Question id: 2, question: "Is Ruby a Scripting Language?", quiz_id: 2, question_permalink: "q1", created_at: "2013-12-28 21:36:25", updated_at: "2013-12-28 21:36:25", order: 1>, :questions_permalink=>nil, :format=>nil} missing required keys: [:questions_permalink]

I see that the Question that I want to view is being set as an object to the :permalink key. Why is that and how would I make it so :permalink is the quiz permalink and :question_permalink is the question peramlink?

Upvotes: 0

Views: 104

Answers (1)

rossta
rossta

Reputation: 11494

Your route requires two parameters: :permalink and :questions_permalink. You'd need to pass an object for each parameter. Based on what you've posted, I presume the following would work:

question_path(quiz, question)

Upvotes: 1

Related Questions