Ruby on rails 为嵌套关联生成多组字段的最简单方法?

Ruby on rails 为嵌套关联生成多组字段的最简单方法?,ruby-on-rails,Ruby On Rails,模型:工作、申请、问题、答案 Jobs has_many questions has many Applications Applications has_many answers belongs_to :job Questions has_one :answer belongs_to :job 在应用程序视图中,我想创建一个视图,列出特定作业的所有问题,并给出相应的答案字段 现在,我可以用黑客的方式做到这一点 我认为最好的方法是同

模型:工作、申请、问题、答案

Jobs has_many questions 
     has many Applications 
Applications has_many answers
             belongs_to :job 
Questions
     has_one :answer
     belongs_to :job
在应用程序视图中,我想创建一个视图,列出特定作业的所有问题,并给出相应的答案字段

现在,我可以用黑客的方式做到这一点

我认为最好的方法是同时运行两个循环,其中一个是一系列的问题,@questions,另一个是fields_for:answers

然后,在标记的字段_中,我可以提供question.id以及question.content

有没有办法做到这一点?

这是我目前的策略

<%= form_for [@job, @application]  do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <% @job.questions.each do |question| %>

    <%= f.fields_for :answers, question do |a| %> #This is the hacked part
      <%= a.label :content, question.content %>
      <%= a.text_area :content, value: "" %>
      <%= a.hidden_field :question_id, value: question.id %> 
    <% end %>

  <% end %>

  <%= f.submit "Submit the application", class: "button" %>
 <% end %>
和应用模型->

class Application < ActiveRecord::Base
    belongs_to :job
    belongs_to :user
    validates :job_id, presence: true 
    validates :user_id, presence: true 

  has_many :questions, through: :job
  has_many :answers, dependent: :destroy

  accepts_nested_attributes_for :answers, allow_destroy: true


   def self.build(job_id)
       application = self.new

       job = Job.find(job_id)
       job.questions.count.times do
           application.answers.build
       end

       application
    end

end
类应用程序
我找到了这个-->


但我不确定这是否可以用一个字段来实现

看起来你的关联有问题。似乎你希望一份工作能够有很多申请和它自己的一套问题。每个申请都会有一套答案,每个答案对应于职位公告中的一个问题。考虑到这一点,您将拥有ff关联:

Job has_many :questions 
     has many :applications 
Application has_many :answers
            belongs_to :job 
Question has_many :answer
          belongs_to :job
Answer belongs_to :application
       belongs_to :question
然后我们在模型中执行此操作:

def build_answers
  job.questions.each do |question|
    application.answers.build(question_id: question.id)
  end
end
在控制器中:

def new 
  @application = Application.new(job_id: params[:job_id])
  @application.build_answers
  redirect_to jobs_path, :notice => "You've already applied to this job! Check out some more" if has_job(current_user,@job)
end
这样,您的视图看起来更简单:

<%= form_for @application do |f| %>
  <%= f.hidden_field :job_id, value: f.object.job_id %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.fields_for :answers do |a| %>
    <% question = a.object.question %>
    <%= a.label :content, question.content %>
    <%= a.text_area :content %>
    <%= a.hidden_field :question_id, value: question.id %> 
  <% end %>

  <%= f.submit "Submit the application", class: "button" %>
<% end %>


希望有帮助

加尔顿。你真是个天才!谢谢你:是DGlad帮了你!顺便说一句,你忘了表格里的东西。为应用程序的job_id添加了一个隐藏的_字段,以便应用程序使用job_id创建。
<%= form_for @application do |f| %>
  <%= f.hidden_field :job_id, value: f.object.job_id %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.fields_for :answers do |a| %>
    <% question = a.object.question %>
    <%= a.label :content, question.content %>
    <%= a.text_area :content %>
    <%= a.hidden_field :question_id, value: question.id %> 
  <% end %>

  <%= f.submit "Submit the application", class: "button" %>
<% end %>