I need some help with nested resource actions. I have three nested resources: Jobs, Questions and Answers. I am currently only trying to get the edit/update method to work for the questions controller. The relationship is as so: Jobs has_many questions and Questions belong to Jobs.
I am using the edit action on the questions and am getting an error:
No route matches "/jobs/1/questions"
and I cannot figure out why.
I currently have this code as my edit and update action in my Questions controller:
def edit
@job = Job.find(params[:job_id])
@question = @job.questions.find(params[:id])
end
def update
@job = Job.find(params[:job_id])
@question = @job.questions.find(params[:id])
if @question.update_attributes(params[:question])
redirect_to(@question)
end
end
Models:
class Job < ActiveRecord::Base
has_many :questions
class Question < ActiveRecord::Base
belongs_to :job
Routes:
resources :jobs do
resources :questions do
resources :answers
end
end
The things that I don't understand are: a) why is it redirecting me to the questions index path, when I didn't redirect it there, and b) It says that is not a valid route, but if I refresh that exact URL the page loads properly.
I have tried multiple options, but I can't figure out the solution.
Thanks for the help. Let me know if you need more info.
p.s. here is my rake routes : https://gist.github.com/1077134