rails嵌套表单与has_many:通过关系

时间:2014-02-02 20:23:26

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord nested-attributes has-many-through

以下是我的模特:

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

项目由雇主创建,学生可以创建project_application。 project_application应该显示问题,然后显示与问题答案对应的表单字段。我不能为我的生活弄清楚表单视图应该如何看。我需要一个form_for ProjectApplication,它接受嵌套的答案属性。我的控制器中有以下内容:

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

这个问题是答案没有正确地与project_applications相关联,因为project_applications还没有id(因为它们还没有被创建)所以关联不会发生,所以答案字段不是显示。这是我现在的视图代码(不起作用):

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

如何更改视图和/或控制器以正确显示与问题和项目应用程序正确关联的答案字段?

1 个答案:

答案 0 :(得分:0)

我的理解:

  1. 您有projects
  2. 每个项目都有很多questions&amp; project_applications
  3. 每个问题都属于project,有很多answersproject_applications

  4. 对于每个项目,您都希望创建具有适用答案的应用程序。这就是我要做的事情:

    <强>模型

    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
    

    <强>控制器

    #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
    

    <强>视图

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

    <强>过程

    这可以通过创建新的project_application,直接向answer模型发送答案来实现。由于answersquestions以及projectsquestions直接相关,因此您应该能够在不传递任何数据的情况下使其正常工作

    这可能无法完全奏效,但我确信通过一些调整可以提供理想的结果