Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 嵌套表单复合体有很多:通过_Ruby On Rails_Nested Forms - Fatal编程技术网

Ruby on rails 嵌套表单复合体有很多:通过

Ruby on rails 嵌套表单复合体有很多:通过,ruby-on-rails,nested-forms,Ruby On Rails,Nested Forms,我有一个复杂的人际关系,用户可以通过应用程序申请工作 创建每个作业时,管理员会选择某些问题。在“新申请”期间,当用户申请新工作时,我还希望用户回答问题以便申请 为此,我将模型设置为: Job has many users through application has many questions Users has_many jobs through application Application belongs_to :user belongs_to :job

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

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

为此,我将模型设置为:

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
我现在有一个应用程序控制器

这就是我被困的地方。如何让用户能够回答有关应用程序的问题#新建视图?

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
这就是到目前为止的情况->

应用程序控制器

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
class ApplicationController“出现问题。请与我们联系并说明此问题”
结束
@job=job.find(@application.job\u id)
@用户=当前用户
结束
结束
控制器基本上会对其进行设置,以便当用户单击submit时,应用程序将具有用户id和作业id,从而有效地建立连接

应用程序#新视图

<%= 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
#==架构信息
#
#表名:应用程序
#
#id:整数不为空,主键
#用户id:integer
#作业id:整数
#创建时间:datetime
#更新时间:datetime
#
类应用程序lambda{a | a[:content].blank?}、:allow_destroy=>true的_嵌套_属性
结束
棘手的问题

这可能不起作用(特别是关联和save参数),但我花了一些时间考虑它,希望它能为您指明正确的方向:


结构

我将创建您的关联,如下所示:

#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

表格

使用,您将能够创建如下表单(使用升级):


我认为你的问题既是一个系统结构问题,也是一个功能问题。要理解嵌套模型提交过程,您将受益于

嘿,Rich,我有点不明白为什么要处理父控制器。作业已经创建,应用程序控制器将简单地将用户链接到作业,以及他们对问题的回答。我想我已经了解了你所说的关于fields_的一些内容,但我可以肯定地看到学习曲线。我会反复阅读你的答案,直到我得到它PHey buddy,我这样写是因为我没有正确理解你的系统。如果把你弄糊涂了,我道歉!我将需要改变我的答案,让它为你工作-这样就已经创造了工作?我猜这意味着用户将回答面试问题,这将绑定到应用程序?哇,谢谢Rich!本质上,应用程序模型是用户和作业之间的链接。为了让用户与作业关联,他们必须创建一个应用程序。我正在重做我的答案,等等!
#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