嵌套表单复杂has_many:通过

时间:2013-12-28 09:59:31

标签: ruby-on-rails nested-forms

我有一个复杂的has_many通关系,用户可以通过应用程序申请工作。

创建每个作业时,管理员会选择某些问题。在Applications#new期间,当用户申请新工作时,我也希望用户回答问题以便申请。

为此,我设置了我的模型:

Job
has many users through application
has many questions 

Users
has_many jobs through application 

Application
    belongs_to :user
    belongs_to :job
    has_many :answers

Question
    belongs_to :job
    has_one :answer

Answer
    belongs_to :application
    belongs_to :question

我现在有一个应用程序控制器。

这是我被困的地方。如何让用户能够回答关于Applications#new view的问题?

这就是目前为止的样子 - >

应用程序控制器

class ApplicationsController < ApplicationController

 def new 
    @application = Application.new() 
    if params[:job_id] # Applications will only be built by clicking on a specific job, which transfers the id of that job 
      @application.job_id = params[:job_id]
      @application.user_id = current_user.id
    else
      redirect_to root_url, :notice => "Something went wrong. Please contact us and mention this"
    end
    @job = Job.find(@application.job_id)
    @user = current_user

 end


end

控制器基本上设置它,以便当用户单击提交时,应用程序将具有用户ID和作业ID,从而有效地建立连接

应用程序#news view

<%= form @application do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <% @job.questions.each do |question| %>
  <h4><%= question.content %></h4>
     #What do I do here to allow the user to answer the question?
  <% end %>
   <%= f.submit "Submit the application", class: "button" %>
<% end %>

应用程序模型

# == Schema Information
#
# Table name: applications
#
#  id         :integer          not null, primary key
#  user_id    :integer
#  job_id     :integer
#  created_at :datetime
#  updated_at :datetime
#

class Application < ActiveRecord::Base
    belongs_to :job
    belongs_to :user
    validates :job_id, presence: true 
    validates :user_id, presence: true 
    has_many :answers
    accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

1 个答案:

答案 0 :(得分:1)

艰难的问题......

这可能不起作用(特别是关联和保存参数),但我花了一些时间思考它,所以希望能指出你正确的方向:


<强>结构

我会按如下方式创建您的关联:

#app/models/job.rb
Class Job < ActiveRecord::Base
    has_many :applications
    has_many :questions
end

#app/models/application.rb
Class Application < ActiveRecord::Base
    belongs_to :user
    belongs_to :job

    has_many :questions, class_name: "Question"
    has_many :answers, class_name: "ApplicationAnswer"

    accepts_nested_attributes_for :answers
end

#app/models/question.rb
Class Question < ActiveRecord::Base
    belongs_to :job
end

#app/models/application_answer.rb
Class ApplicationAnswer < ActiveRecord::Base
    belongs_to :application
    belongs_to :question
end

<强>架构

jobs
id | name | info | created_at | updated_at

applications
id | user_id | job_id | created_at | updated_at

questions
id | job_id | created_at | updated_at

answers
id | application_id | question_id | answer_info | created_at | updated_at 

<强>路线

我会创建嵌套路由,以便您只能为特定作业创建应用程序:

#config/routes.rb
resources :jobs do 
     resources :applications
end

<强>控制器

#app/controllers/applications_controller.rb
def new
    @job = Job.find(params[:job_id])

    @application = Application.new
    @job.questions.count.times do { @application.answers.build } #-> gives you fields equivalent for number of questions per job 
end

<强>表格

使用accepts_nested_attributes_for,您将能够按如下方式创建表单(使用this idea升级):

#app/views/applications/new.html.erb
<%= form_for @application do |f| %>
    <% @job.questions.each_with_index do |question| %>
         <%= f.fields_for :answers, question do |question_field| %>
              <%= question_field.label_for :answer, question.text %>
              <%= question_field.text_field :answer %>
              <%= question_field.hidden_field :question_id, question.id %>
         <% end %>
    <% end %>
<% end %>

<强>保存

#app/controllers/applications_controller.rb
def create
    @application = Application.new(application_params)
    @application.save
end

private
def application_params
    params.require(:application).permit(:job_id, :user_id, answers_attributes:[:answer, :question_id]).merge(user_id: current_user.id)
end

我认为您的问题与系统结构问题一样多。要了解嵌套模型提交过程,您可以从watching this Railscast

中受益