嵌套属性不会在Rails中更新 [英] Nested attributes are not updating in rails

查看:119
本文介绍了嵌套属性不会在Rails中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种型号:

class DropShipOrderLineItem < ActiveRecord::Base
  belongs_to :drop_ship_order
  belongs_to :line_item

  validates_associated :drop_ship_order
  validates_associated :line_item
  validates :drop_ship_order_id, :presence => true
  validates :line_item_id, :presence => true

  attr_accessible :missing

end

class DropShipOrder < ActiveRecord::Base

  attr_accessible :line_items, :line_items_attributes, :orders, :order_attributes

  belongs_to  :retailer
  belongs_to  :order
  belongs_to :shipping_method

  has_many    :drop_ship_order_line_items
  has_many    :line_items, :through => :drop_ship_order_line_items
  has_many    :shipments, :dependent => :destroy

  accepts_nested_attributes_for :line_items
  accepts_nested_attributes_for :order
  accepts_nested_attributes_for :drop_ship_order_line_items

  before_create :generate_order_number

  scope :by_number, lambda {|number| where("drop_ship_orders.number = ?", number)}
  scope :between, lambda {|*dates| where("drop_ship_orders.created_at between ? and ?", dates.first.to_date, dates.last.to_date)}
  scope :by_customer, lambda {|customer| joins(:order).joins(:user).where("users.email =?", customer)}
  scope :by_state, lambda {|state| where("state = ?", state)}
  scope :complete, where("drop_ship_orders.completed_at IS NOT NULL")
  scope :incomplete, where("drop+ship_orders.orders.completed_at IS NULL")

  make_permalink :field => :number

  validates_associated :order
  validates_associated :retailer
end

LineItem.class_eval do

  has_one :drop_ship_order_line_item, :dependent => :destroy
  has_one :drop_ship_order, :through => :drop_ship_order_line_items

  accepts_nested_attributes_for :drop_ship_order_line_item

  attr_accessible :drop_ship_order_line_item

#  def missing
#    self.drop_ship_order_line_item.missing
#  end

end

查看#1:

<div data-hook="admin_order_edit_form">
  <div id="order-form-wrapper">
    <%= render :partial => 'form', :locals => {:drop_ship_order => @drop_ship_order} %>
  </div>
</div>

查看表单:

<%= form_for(@drop_ship_order, :url => admin_drop_ship_order_path(@drop_ship_order), :html => { :method => :put}) do |f| %>
  <%= f.hidden_field :number %>
  <table class="index">
    <tbody id='line-items'>
      <tr data-hook="admin_order_form_line_items_headers">
        <th><%= t('item_description') %></th>
        <th class="price"><%= t('price') %></th>
        <th class="qty"><%= t('qty') %></th>
        <th class="total"><span><%= t('total') %></span></th>
        <th class="orders-actions" data-hook="admin_order_form_line_items_header_actions"></th>
      </tr>
      <%= f.fields_for :line_items do |li_form| %>
        <%= render :partial => "admin/drop_ship_orders/line_item", :locals => { :f => li_form } %>
      <% end %>
    </tbody>
    <tbody id='subtotal' data-hook="admin_order_form_subtotal">
      <tr id="subtotal-row">
        <td colspan="3"><b><%= t('subtotal') %>:</b></td>
        <td class="total"><span><%= number_to_currency @drop_ship_order.item_total %></span></td>
        <td></td>
      </tr>
    </tbody>

查看line_item:

view line_item:

<tr id="<%= dom_id(f.object) %>" data-hook="admin_order_form_line_item_row">
  <td width="300"><%=f.object.variant.product.name%> <%= "(" + variant_options(f.object.variant) + ")" unless f.object.variant.option_values.empty? %></td>
  <td valign="top" class="price"><%= number_to_currency f.object.price %></td>
  <td valign="top" class="qty"><strong><%= f.object.quantity %></strong></td>
  <td valign="top" class="total"><%= number_to_currency (f.object.price * f.object.quantity)%></td>
  <td data-hook="admin_order_form_line_item_actions">
    <!--<input type="checkbox" name="apple" id="apples"/>-->
    <%#= f.object.drop_ship_order_line_item.missing %>
    <%= f.fields_for :drop_ship_order_line_item do |build|%>
        <%= build.check_box :missing, :type => "checkbox" %> 
    <% end %>

  </td>
</tr>

:missing属性没有被更新,我只是不知道为什么!尽管如果我手动更改SQL表中的值,则复选框已正确打勾.

The :missing attribute is not being updated, and I just can't figure out why! Although the checkboxe is correctly being ticked if I manually change the values in my SQL table.

p.s:我刚刚发布了代码的相关部分.

p.s: I just posted the relevant parts of my code.

推荐答案

在LineItem模型中,将attr_accessible :drop_ship_order_line_item更改为attr_accessible :drop_ship_order_line_item_attributes.

In your LineItem model change attr_accessible :drop_ship_order_line_item to attr_accessible :drop_ship_order_line_item_attributes.

这篇关于嵌套属性不会在Rails中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆