嵌套表单复杂has_many:通过

我有一个复杂的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  "Something went wrong. Please contact us and mention this" end @job = Job.find(@application.job_id) @user = current_user end end 

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

应用程序#new view

    

#What do I do here to allow the user to answer the question?

应用模型

 # == 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  lambda { |a| a[:content].blank? }, :allow_destroy => true end 

棘手的问题….

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


结构体

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

 #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 ,您将能够创建如下表单(使用此想法升级):

 #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 

我认为你的问题与系统结构问题一样多。 要了解嵌套模型提交过程,您可以从观看此Railscast中受益