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 route开发者_开发知识库s : https://gist.github.com/1077134
To get you started, In view/jobs/show.rb :
<%= link_to 'Edit', edit_jobs_path(@job) %>
In view/questions/show.rb :
<%= link_to 'Edit', edit_job_question_path(@question.job, @question) %>
In view/questions/edit.rb :
<%= link_to 'Show', job_question_path %>
What I'm showing is that the links need to have a nested pattern. If your answers had many comments, you might end up with things like: edit_job_question_answer_comment(@job, @question, @answer, @comment) where the @symboled variables are derived in the controller. Hope this helps!
You may later want:
class Job < ActiveRecord::Base
has_many :questions
has_many :answer, :through => :questions
# If you want to edit the questions of a job whilst editing a job then research accepts nested attributes
#accepts_nested_attributes_for :questions, :allow_destroy => true
end
So it turns out that my issue was a little more involved than I had originally thought. My database and tables were not setup properly and they were having trouble finding the proper :ids for my resources. I had to start by normalizing my tables like so:
class CreateQuestions < ActiveRecord::Migration
def self.up
create_table :questions do |t|
t.references :job
t.text :question1
t.text :question2
t.text :question3
t.text :question4
t.text :question5
t.text :question6
t.text :question7
t.text :question8
t.text :question9
t.text :question10
t.timestamps
end
end
This set up was repetitive and dirty and it was messing up questions controller actions. So I changed it to:
def self.up
create_table :questions do |t|
t.references :job
t.text :question
t.timestamps
end
end
and created nested_forms with loops in my jobs (the parent resource) new_form view.
<%= form_for(@job) do |f| %>
<%= f.label :name %><br />
<%= f.text_field :name %>
<%= f.fields_for :questions do |builder| %>
<%= f.label :question, "Question" %><br \>
<%= f.text_area :question, :rows => 10 %>
<% end %>
After doing this all of my controller methods were cleaner and the edit/update action was working properly.
This is how I solved the issue, it may not be the best way to do so. Also, if you have anything to add or any questions about my code, just let me know and I'll see if I can help.
Thanks!
精彩评论