Magofoco
Magofoco

Reputation: 5466

Simple form for in nested routes

I am building a website where a user can create a project. Each project contains several papers. And each paper can be "graded" by submitting a form that asks several questions.

I am trying to display a simple_form to show the questions but I am failing to do so.

When I go to: http://0.0.0.0:3000/projects/10/papers/31/answering_questions

I get the following error:

My error

I know that the correct paper and project are passed in /answering_questions because if I write <%= @paper.title %>, the correct title of the paper is displayed.

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>

My Routes:

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

Upvotes: 1

Views: 84

Answers (1)

Vasilisa
Vasilisa

Reputation: 4640

If you don't mind, I'll convert my comment to the answer.

Looks like you forgot to define @project for the answering_questions action. Since it equals to nil, simple_form_for [@project, @paper] builds paper_path, instead of project_paper_path.

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

Upvotes: 1

Related Questions