Rails link_to问题

时间:2014-02-21 19:48:06

标签: ruby-on-rails

我是rails的新手,并且一直致力于创建一个基本的调查应用程序来处理嵌套资源。

以下是我最终的数据结构:

  1. 调查

  2. 问题,有外国调查

  3. 回答,提出异议

  4. 选择,参与调查的用户的外国人和他将选择的回答ID

  5. 我能够创建调查和问题结构。但是我的答案遇到了一些麻烦。它在我的问题的show.html.erb文件的最后一行引发了一个错误,我正在尝试link_to我的新答案。

    这是问题的show.html.erb文件:

    <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>
    

    这是我的routes.rb:

    SurveyMe::Application.routes.draw do
    
      resources :surveys do
        resources :questions do
          resources :answers
        end
      end
    
      devise_for :developers
      devise_for :users
    

    我很确定问题是[@survey,@question]这一行。有什么想法我应该放在那里吗?

    这是错误:

     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>
    

1 个答案:

答案 0 :(得分:1)

那应该是: new_survey_question_answer_path(@survey, @question)