Note that there are some explanatory texts on larger screens.

plurals
  1. POLink_to Routing Issue With Nested Resources
    primarykey
    data
    text
    <p>I have two models Jobs and Questions. A job has many questions and questions belong to a job. I've set up the resources in the model, as well as the routes. I am having an issue trying to link_to the Show method of the questions controller on the questions#index page. My rake routes say that the path should be job_question_path with the two necessary :id's being :job_id and :id , so I tried:</p> <pre><code>&lt;td&gt;&lt;%= link_to 'Show', job_question_path(@job, question) %&gt;&lt;/td&gt; </code></pre> <p> </p> <p>and got the error:</p> <pre><code>No route matches {:action=&gt;"show", :controller=&gt;"questions", :job_id=&gt;nil, :id=&gt;#&lt;Question id: 1, job_id: 1, question1: "sfsdfssfs", question2: "sfsdfs", question3: "sfsdf", question4: "sfsdfsf", question5: "sfsfsfs", created_at: "2011-06-21 03:25:12", updated_at: "2011-06-21 03:25:12"&gt;} </code></pre> <p>I've tried multiple other combos and nothing is seeming to work, I keep getting:</p> <pre><code>No route matches {:action=&gt;"show", :controller=&gt;"questions", :job_id=&gt;nil } </code></pre> <p>or some combination of that.</p> <p>The part I don't get is that I can put in the url /jobs/1/questions/1 and it takes me to the show page, so I am assuming that my questions#show methods are ok. See below for the rest of my code. </p> <p>Questions#index view</p> <pre><code>&lt;% @questions.each do |question| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= question.question1 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= question.question2 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= question.question3 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= question.question4 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= question.question5 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Show', job_question_path(@job, question) %&gt;&lt;/td&gt; &lt;/tr&gt; </code></pre> <p>&lt;% end %></p> <p>Questions Controller</p> <pre><code>def index @questions = Question.all respond_to do |format| format.html # index.html.erb format.xml { render :xml =&gt; @questions } end end def show @job = Job.find(params[:job_id]) @question = @job.questions.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml =&gt; @question } end end </code></pre> <p>Models</p> <pre><code>class Job &lt; ActiveRecord::Base has_many :questions class Question &lt; ActiveRecord::Base belongs_to :job </code></pre> <p>Routes.rb</p> <pre><code> root :to =&gt; "pages#home" resources :jobs do resources :questions end get "pages/home" get "pages/about" get "pages/contact" </code></pre> <p>See this <a href="https://gist.github.com/1032734" rel="nofollow">https://gist.github.com/1032734</a> for my rake routes. </p> <p>Thanks for any help in advance, i've been at this for a while now and just can't figure out the solution. Please let me know if you need any more info. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload