Tom Hammond
Tom Hammond

Reputation: 6080

Rails link_to issue

I'm new to rails and have been working on creating a basic survey app to work through nested resources.

Here's what my eventual data structure should look like:

  1. survey

  2. question, with a foreign of survey

  3. answer, with a foreign of question

  4. choice, with a foreign of the user who is taking the survey and answer id which he will select

I was able to create the survey and question structure. But I'm running into some trouble with the answers. It's throwing an error on the last line in my question's show.html.erb file where I'm trying to link_to my new answer.

Here's the question's show.html.erb file:

<div id='question'>
    <h2><%= @question.title %></h2>
    <% if @question.single_response %>
    <p>Single response</p>
    <% else %>
    <p>Multiple response</p>
    <% end %>
</div>
<%= link_to "Edit Question", [:edit, @survey, @question] %>
<%= link_to "Delete Question", [@survey, @question], method: :delete, data: { confirm: "Are you sure you want to delete this question?"} %>

<p>Answers</p>
<ul id='answers'>
    <% @question.answers.each do |answer| %>
    <li><%= link_to answer.title, [@survey, @question, answer] %></li>
    <% end %>
</ul>
<p><%= link_to "Add Answer", new_survey_question_answer_path([@survey,@question]) %></p>

Here's my routes.rb:

SurveyMe::Application.routes.draw do

  resources :surveys do
    resources :questions do
      resources :answers
    end
  end

  devise_for :developers
  devise_for :users

I'm pretty sure the issue is the [@survey, @question] piece of the line. Any ideas what I should be putting in there?

Here's the error:

 Showing /Users/thomashammond89/Test Survey.me/app/views/questions/show.html.erb where line #18 raised:

No route matches {:action=>"new", :controller=>"answers", :survey_id=>[#<Survey id: 2, title: "Test1", created_at: "2014-02-21 17:35:36", updated_at: "2014-02-21 17:35:36">, #<Question id: 2, title: "Question Test1", single_response: true, survey_id: 2, created_at: "2014-02-21 18:59:57", updated_at: "2014-02-21 18:59:57">], :id=>"2", :question_id=>nil, :format=>nil} missing required keys: [:question_id]

Extracted source (around line #18):

15 <li><%= link_to answer.title, [@survey, @question, answer] %></li>
16 <% end %>
17 </ul>
18 <p><%= link_to "Add Answer", new_survey_question_answer_path([@survey,@question]) %></p>

Upvotes: 0

Views: 73

Answers (1)

Hesham
Hesham

Reputation: 2347

That should be: new_survey_question_answer_path(@survey, @question)

Upvotes: 1

Related Questions