简体   繁体   English

Rails link_to问题

[英]Rails link_to issue

I'm new to rails and have been working on creating a basic survey app to work through nested resources. 我是Rails的新手,一直在努力创建一个基本的测量应用程序以处理嵌套资源。

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 选择,与正在调查的用户的外国人和他将选择的答案ID

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. 在我的问题的show.html.erb文件的最后一行,我试图将其链接到新答案时,抛出了错误。

Here's the question's show.html.erb file: 这是问题的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>

Here's my routes.rb: 这是我的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. 我敢肯定问题出在这行的[@survey,@question]。 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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM