Ruby on rails 是否可以在一个视图中从两个窗体创建两个对象,并使它们相互关联?
我知道这个标题听起来有点不靠谱,但这正是我想要做的 我有两种模式-Ruby on rails 是否可以在一个视图中从两个窗体创建两个对象,并使它们相互关联?,ruby-on-rails,simple-form,ruby-on-rails-5,Ruby On Rails,Simple Form,Ruby On Rails 5,我知道这个标题听起来有点不靠谱,但这正是我想要做的 我有两种模式-工作和公司。有人可以创建工作列表,但在保存该列表之前,它应该与公司关联。此公司可以是新创建的,也可以从当前用户先前创建的公司填充 一个工作属于:公司,而一个公司有许多:工作 我知道我可以做两个不同的视图,只需在两个不同的控制器上向用户发送两个不同的操作,但我想简化它,只需在一个视图中完成所有操作 当他们转到/jobs/new时,他们应该会看到正常的jobs/_form.html.erb部分,但我希望能够显示现有公司的公司下拉列表或
工作
和公司
。有人可以创建工作列表,但在保存该列表之前,它应该与公司关联。此公司可以是新创建的,也可以从当前用户先前创建的公司填充
一个工作属于:公司
,而一个公司有许多:工作
我知道我可以做两个不同的视图,只需在两个不同的控制器上向用户发送两个不同的操作,但我想简化它,只需在一个视图中完成所有操作
当他们转到/jobs/new
时,他们应该会看到正常的jobs/_form.html.erb
部分,但我希望能够显示现有公司的公司下拉列表或用户填写的新公司创建字段,并且新公司与正在创建的新工作关联
这就是我的jobs/_form.html.erb
partial看起来的样子:
<%= simple_form_for(@job) do |f| %>
<%= f.input_field :title %>
<%= f.input_field :salary %>
<%= f.input_field :description, as: :text %>
<%= f.input_field :apply_description, as: :text %>
<%= f.input_field :language %>
<%= f.input :premium, as: :boolean, inline_label: true, label: "Make this listing stand out", class: "form-control" %>
<%= f.button :submit, class: "btn btn-lg btn-primary pull-right" %>
<% end %>
<%= simple_form_for(@company) do |f| %>
<%= f.input :name %>
<%= f.input :logo %>
<%= f.input :description %>
<%= f.input :city %>
<%= f.input :state %>
<%= f.input :country %>
<%= f.input :email %>
<%= f.button :submit %>
<% end %>
如何将它们组合到一个表单中,或在一个视图中的其他统一工作流中,使其与用户无缝工作
编辑1
根据Jay Ar的回答,这就是现在正在发生的事情
当我在“公司”下拉列表中选择“新建公司”时,它不会显示“公司”下拉列表中的字段。我相信是这样的,因为HTML中呈现的select标记中没有value=0
,如下面的屏幕截图所示
编辑2
在尝试Jay Ar的最新更新后,JS仍然无法工作,表单也不再隐藏
这是第一次加载时的外观,并且始终是:
理想情况下,我希望在他们从下拉列表中选择“新公司”之前,不要显示此表格
这就是HTML现在的样子:
JS确实出现在源代码中,因此我知道它正在正确地加载到资产管道中。您应该使用嵌套模型表单,我建议您注意这一点。它非常详细地解释了为问答模型执行此操作的步骤,但这是同一件事。您应该使用嵌套模型表单,我建议你看这篇文章,它有一个非常详细的步骤说明,用于问答模型,但它是一样的。更新和测试工作
- 现在支持涡轮链接
<%= simple_form_for(@job) do |f| %>
<%# IMPORTANT: use `include_blank` below instead of `prompt` because prompt does not seem to work when updating, but only works when creating %>
<%= f.association :company, collection: [['New Company', nil]] + Company.pluck(:name, :id), include_blank: 'Please Select Company', input_html: { id: 'company-select' } %>
<fieldset id='job-fields'>
<%= f.simple_fields_for :company, @job.build_company do |ff| %>
<%= ff.input :name %>
<%= ff.input :logo %>
<%= ff.input :description %>
<%= ff.input :city %>
<%= ff.input :state %>
<%= ff.input :country %>
<%= ff.input :email %>
<% end %>
</fieldset>
<%= f.input_field :title %>
<%= f.input_field :salary %>
<%= f.input_field :description, as: :text %>
<%= f.input_field :apply_description, as: :text %>
<%= f.input_field :language %>
<%= f.input :premium, as: :boolean, inline_label: true, label: "Make this listing stand out", class: "form-control" %>
<%= f.button :submit, class: "btn btn-lg btn-primary pull-right" %>
<% end %>
控制器/jobs\u controller.rb
class JobsController < ApplicationController
...
def create
@job = Job.new(job_params)
...
end
def update
@job = Job.find(params[:id]) # not needed if using before_action #set_job
if @job.update(job_params)
...
end
private
def job_params
params.require(:job).permit(:id, :title, :salary, :description, :apply_description, :language, :premium, :company_id, company_attributes: [:name, :logo, :description, :city, :state, :country, :email]
end
end
类作业控制器
models/job.rb
class Job < ActiveRecord::Base
accepts_nested_attributes_for :company
validates :company, presence: true
...
end
类作业
您应该得到如下信息:
- 新装货时:
- 选择“新公司”选项后:
更新和测试工作
- 现在支持涡轮链接
<%= simple_form_for(@job) do |f| %>
<%# IMPORTANT: use `include_blank` below instead of `prompt` because prompt does not seem to work when updating, but only works when creating %>
<%= f.association :company, collection: [['New Company', nil]] + Company.pluck(:name, :id), include_blank: 'Please Select Company', input_html: { id: 'company-select' } %>
<fieldset id='job-fields'>
<%= f.simple_fields_for :company, @job.build_company do |ff| %>
<%= ff.input :name %>
<%= ff.input :logo %>
<%= ff.input :description %>
<%= ff.input :city %>
<%= ff.input :state %>
<%= ff.input :country %>
<%= ff.input :email %>
<% end %>
</fieldset>
<%= f.input_field :title %>
<%= f.input_field :salary %>
<%= f.input_field :description, as: :text %>
<%= f.input_field :apply_description, as: :text %>
<%= f.input_field :language %>
<%= f.input :premium, as: :boolean, inline_label: true, label: "Make this listing stand out", class: "form-control" %>
<%= f.button :submit, class: "btn btn-lg btn-primary pull-right" %>
<% end %>
控制器/jobs\u controller.rb
class JobsController < ApplicationController
...
def create
@job = Job.new(job_params)
...
end
def update
@job = Job.find(params[:id]) # not needed if using before_action #set_job
if @job.update(job_params)
...
end
private
def job_params
params.require(:job).permit(:id, :title, :salary, :description, :apply_description, :language, :premium, :company_id, company_attributes: [:name, :logo, :description, :city, :state, :country, :email]
end
end
类作业控制器
models/job.rb
class Job < ActiveRecord::Base
accepts_nested_attributes_for :company
validates :company, presence: true
...
end
类作业
您应该得到如下信息:
- 新装货时:
- 选择“新公司”选项后:
我真的很喜欢你要去的地方。JS似乎不适合我。当我在“选择”下拉列表中选择“新公司”选项时,我没有看到
中的字段出现。我想我知道这是为什么。根据你的建议刷新问题以查看我的更新。我对jobs/\u form.html.erb和JS解决了我在复制场景时遇到的一些问题。我已经测试过了,但仍然不起作用:(我更新了问题的更多细节,说明了这些最新更改后发生的情况。@marcamillion我更新了我的答案,以处理您的请求,使“新公司”字段仅在选中时显示。对此进行了测试,结果也正常。但这仍然无法解决您的问题。您能否检查实际功能是否正常调用ng?比如,您可以在开始时添加alert('IM-INSIDE-PAGE:CHANGE')
,即$(文档)。在('PAGE:CHANGE',function(){alert('IM-INSIDE-PAGE:CHANGE');
上,您可以将其修改为类似以下内容吗?companySelect.CHANGE(function(){alert('IM INSIDE companySelect-CHANGE function'))
告诉我这些是否被调用我真的很喜欢你要去的地方。JS似乎不适合我。当我在“选择”下拉列表中选择“新公司”选项时,我看不到
中的字段显示。我想我知道这是为什么。根据你的建议刷新问题以查看我的更新。我做了很多更改