Question

Here are my models:

class Project < ActiveRecord::Base
  has_many :project_applications
  has_many :questions
  accepts_nested_attributes_for :questions,  :allow_destroy => true, :reject_if => proc { |a| a[:content].blank? }
end

class Question < ActiveRecord::Base
  belongs_to :project
  has_many :answers
  has_many :project_applications, through: :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
  belongs_to :project_application
end

class ProjectApplication < ActiveRecord::Base
  belongs_to :project
  belongs_to :student
  has_many :answers
  has_many :questions, through: :answers
end

A project is created by an employer, and a student can create a project_application. The project_application should present the questions and then show form fields that correspond to the questions answers. I cannot for the life of me figure out how the form view should look. I need a form_for ProjectApplication that accepts nested attributes for answers. I have the following in my controller:

class ProjectApplicationsController < ApplicationController
    def new
        @project = Project.find(params[:project_id])
        @project_application = ProjectApplication.new
        @project_application.project = @project  

        @project_application.project.questions.each do |question|
            @answer = question.answers.build
            @answer.project_application = @project_application  #this line does not work
            puts 'answer' + @answer.inspect.to_s
        end

        puts 'here are the answers' + @project_application.answers.inspect.to_s

    end
end

The problem with this is that the answers are not correctly being associated with project_applications because the project_applications don't have an id yet (because they have not been created) so the association can't happen, so the answer fields are not displayed. Here is the view code (does not work) that I have now:

<%= form_for @project_application, url: project_project_applications_path(@project.id), method: :post, remote: true do |f| %>
    <%= f.fields_for :project do |proj| %>
        <%= proj.fields_for :questions do |quest| %>
            <%= quest.fields_for :answers do |answer| %>
                <%= answer.text_area :content %>
            <% end %>
        <% end %>
    <% end %>

    <%= f.submit "APPLY" %>

<% end %>

How do I change the view and/or controller to properly display answer fields correctly associated with questions and the project application?

Was it helpful?

Solution

My understanding:

  1. You have projects
  2. Each project has many questions & project_applications
  3. Each question belongs to a project, has many answers through project_applications

For each project, you'd like to create applications with the applicable answers. Here's what I'd do:

Models

class Project < ActiveRecord::Base
  has_many :project_applications
  has_many :questions
end

class Question < ActiveRecord::Base
  belongs_to :project
  has_many :answers
  has_many :project_applications, through: :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
  belongs_to :project_application
end

class ProjectApplication < ActiveRecord::Base
  belongs_to :project
  belongs_to :student
  has_many :answers
  has_many :questions, through: :project

  accepts_nested_attributes_for :answers

  def self.build
      application = self.new
      application.answers.build
  end
end

Controller

#app/controllers/project_applications_controller.rb
def new
    @projectapplication = ProjectApplication.build
    @project = @projectapplication.project
end

def create

end

private
def application_params
    params.require(:project_application).permit(:application, :attributes, :here, answer_attributes: [:content])
end

Views

#app/views/project_applications/new.html.erb
<%= form_for @projectapplication do |f| %>
    <%= f.text_field :application_fields %>

    <% @project.questions.each do |question| %>
        <%= f.fields_for :answers, question do |answer| %>
            <%= answer.hidden_field :question_id, question.id %>
            <%= answer.text_field :content, placeholder: question.content %>
        <% end %>
    <% end %>

<% end %>

Process

This works by creating a new project_application, sending answers directly to the answer model. Because answers are directly associated with questions, and projectsthrough questions, you should be able to get it working without passing any more data

This might not work outright, but I'm sure with some tweaking it will deliver the desired result

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top