[英]rails nested form with has_many :through relationship
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. 项目是由雇主创建的,学生可以创建project_application。 The project_application should present the questions and then show form fields that correspond to the questions answers.
project_application应该显示问题,然后显示与问题答案相对应的表单字段。 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.
我需要一个form_for ProjectApplication接受嵌套属性的答案。 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. 问题是答案没有正确地与project_applications关联,因为project_applications还没有ID(因为尚未创建),因此关联无法发生,因此不会显示答案字段。 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? 如何更改视图和/或控制器以正确显示与问题和项目应用程序正确关联的答案字段?
My understanding: 我的理解:
projects
projects
questions
& project_applications
questions
和project_applications
project
, has many answers
through project_applications
project
,通过project_applications
有很多answers
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. 通过创建一个新的
project_application
,将答案直接发送到answer
模型来工作。 Because answers
are directly associated with questions
, and projects
through questions
, you should be able to get it working without passing any more data 由于
answers
与questions
直接相关,并且通过questions
projects
,因此您应该能够使它正常工作而无需传递任何其他数据
This might not work outright, but I'm sure with some tweaking it will deliver the desired result 这可能无法完全解决问题,但是我敢肯定,通过一些调整,它可以达到预期的效果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.