按输入表格编辑相关模型?

时间:2017-06-02 03:26:13

标签: ruby-on-rails ruby ruby-on-rails-4

Order has_many jobs
Job belongs to order

我想编辑@job.order

的属性
<% order = @job.order %>
<%= simple_form_for [@job, order],
  url: job_path(@job),
  method: :put,
  remote: true do |f| %>
    <%= f.input :order_status, input_html: {class: 'form-control'} %>
  (...)
<% end %>

只需使用简单形式的输入即可实现此目的吗?

2 个答案:

答案 0 :(得分:0)

在job.rb

accepts_nested_attributes_for :order

form.html.erb

中的

simple_form_for @job do |f|
  f.simple_fields_for @job.order do |order_form|
    order_form.input :status
  end    
end
jobs_controller.rb

中的

params.require(:job).permit(:something, :something_else, :order_attributes => [:status])

答案 1 :(得分:0)

您可以使用优秀的Cocoon gem https://github.com/nathanvda/cocoon来管理嵌套关系,包括轻松添加新嵌套关系的功能。

<table id="productsTable">
</table>

请注意复数。

<强> _form.html.erb *

class Job < ActiveRecord::Base
    has_many :orders
    accepts_nested_attributes_for :orders, reject_if: :all_blank, allow_destroy: true
end

class Order < ActiveRecord::Base
    belongs_to :job
end

_order_fields.html.erb 部分

<%= form_for @job do |f| %>
  <%= f.label :job_name %>
  <%= f.text_field :name %>
  <div id='order'>
    <%= f.fields_for :orders do |order| %>
       <%= render 'order_fields', f: order %>
    <% end %>
    <div class='links'>
       <%= link_to_add_association 'add order', f, :orders %>
    </div>
   <%= f.submit %>
 <% end %>