嵌套路线的简单形式

时间:2019-02-03 12:01:20

标签: ruby-on-rails

我正在建立一个用户可以在其中创建项目的网站。每个项目包含几篇论文。通过提交询问几个问题的表格,可以对每篇论文进行“评分”。

我试图显示 simple_form 来显示问题,但我没有这样做。

当我转到:http://0.0.0.0:3000/projects/10/papers/31/answering_questions

我收到以下错误:

My error

我知道在/answering_questions中传递了正确的论文和项目,因为如果我写<%= @paper.title %>,则会显示正确的论文标题。

answering_questions.html.erb

<h1>Answering questions</h1>

<h4>You are answering the questions for the paper:</h4>
<%= @paper.title %>

<div class="container">
  <div class="row ">
    <div class="col-sm-6 col-sm-offset-3">
      <%= simple_form_for [@project, @paper] do |f| %>
      <%= f.input :question_1, :collection =>["N/A", "No - 0", "Partially - 0.5", "Yes - 1"], label: "Question 1" %>
      <%= f.input :question_2, :collection =>["N/A", "No - 0", "Partially - 0.5", "Yes - 1"], label: "Question 2" %>
      <%= f.input :question_3, :collection =>["N/A", "No - 0", "Partially - 0.5", "Yes - 1"], label: "Question 3" %>
      <%= f.association :project, :as => :hidden, label: "To which project are you adding it?", :input_html => { :value => @project }  %>
    </div>
    <div class="form-actions">
      <%= f.button :submit, label: "Send your review" %>
    </div>
    <% end %>
  </div>
</div>

我的路线:

Rails.application.routes.draw do
  resources :projects do
    resources :papers do
      member do
        get "answering_questions"
      end
    end
  end
  devise_for :users
  root to: 'pages#home'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

1 个答案:

答案 0 :(得分:1)

如果您不介意,我会将评论转换为答案。

好像您忘记为@project动作定义answering_questions。由于它等于nil,因此simple_form_for [@project, @paper]生成paper_path而不是project_paper_path

def answering_questions 
  @project = Project.find(params[:project_id]) 
  @paper = Paper.find(params[:id])
end
相关问题