Ruby on Rails" Form"插入关联表

时间:2015-11-10 13:39:42

标签: ruby-on-rails ruby activerecord

我正在构建一个包含2个表的订单管理系统。 :customer:order。我可以销毁,展示和编辑。但我只能为客户创造,而不是为它带来的订单。这是我到目前为止的代码:

class Order < ActiveRecord::Base
  belongs_to :customer
end

class Customer < ActiveRecord::Base
  has_many :orders
end
def new
    @orders = Order.new
    @customer = Customer.new ({:voornaam => "ABC"})
 end

 def create
    @customer = Customer.new(customer_params)
    if @customer.save
    orders = Order.where(params(order_params))
    @customer.orders << orders
    flash[:notice] = "Subject created successfully"
    redirect_to(:action => 'index')
    else
    render('new') 
  end
end


def customer_params
    #same as using "params[:subject]", expect that it:
    # - raises an error if :subject is not present
    # - allows listed attributes to be mass-assigned
   params.require(:customer).permit(:voornaam, :achternaam)
  end

  private

  def order_params
    #same as using "params[:subject]", expect that it:
    # - raises an error if :subject is not present
    # - allows listed attributes to be mass-assigned
   params.require(:customer).permit(:pakket, :datum_bestelt, :verstuurt, :datum_verstuurt, :tweede_exemplaar, :packettracer_ID)
  end
end

创建订单

<%= form_for(:customer, :url=> {:action => 'create'}) do |f| %>

<table summary="subject form fields">
    <tr>
        <th>firstname</th>
        <td><%= f.text_field(:voornaam) %></td>
    </tr>
    <tr>
        <th>lastname</th>
        <td><%= f.text_field(:achternaam) %></td>
    </tr>
    <%= f.fields_for :order do |s| %>
    <tr>
        <th>pakket</th>
        <td><%= s.text_field(:pakket) %></td>
    </tr>
    <tr>
        <th>datum_bestelt</th>
        <td><%= s.text_field(:datum_bestelt) %></td>
    </tr>
    <tr>
        <th>verstuurt</th>
        <td><%= s.text_field(:verstuurt) %></td>
    </tr>
    <tr>
        <th>datum_verstuurt</th>
        <td><%= s.text_field(:datum_verstuurt) %></td>
    </tr>
    <tr>
        <th>tweede_exemplaar</th>
        <td><%= s.text_field(:tweede_exemplaar) %></td>
    </tr>
    <tr>
        <th>packettracer_ID</th>
        <td><%= s.text_field(:packettracer_ID) %></td>
    </tr>
    <% end %>

</table>

2 个答案:

答案 0 :(得分:2)

要在同一请求中创建客户和订单,您可以使用nested attributes

class Order
  belongs_to :customer
  accepts_nested_attributes_for :customer
end

这可以让您通过以下方式创建客户:

Order.create(customer_attributes: { voornaam: 'Max' })

在表单中,您可以像这样添加输入:

<%= form_for(:order) do |f| %> 
  <%= f.fields_for :customer do |cf| %>
    <%= cf.text_field :voornaam %>
  <% end %>
<% end %>

这就是你将这些参数列入白名单的方式:

def order_params
  params.require(:order)
        .permit(
           :foo, :bar, 
           customer_params: [
             :voornaam,
             # ...
           ]
         )
end

但是,从用户体验的角度来看,将其拆分为单独的步骤可能会更好。

在用户创建帐户并登录之前,您可能需要考虑与订单关联的having a guest user记录。

答案 1 :(得分:2)

您的控制器中此行可能存在问题:

orders = Order.where(params(order_params))
@customer.orders << orders

您实际上是在尝试在数据库中提取与您的params相对应的订单,并将其链接到@customer。因此,如果您想使用params创建新订单,则无法找到任何内容,因此orders始终为空。然后,您将一个空数组附加到@customer.orders

你能做的是:

orders = @customer.orders.create(order_params)