0

我有一个购物车,在我的展示购物车页面中,我试图让用户灵活地更新数量,我希望只有一个数量字段是可编辑的。这是我的代码

class Cart < ActiveRecord::Base
  # attr_accessible :title, :body
  has_many :line_items, dependent: :destroy

class LineItem < ActiveRecord::Base
  belongs_to :product 
  belongs_to :cart
  #before_save :quantity
  attr_accessible :cart_id, :product_id, :quantity


class Product < ActiveRecord::Base
  has_many :photos, :dependent => :destroy
  has_many :line_items
  accepts_nested_attributes_for :photos, :line_items

在我看来

<p id="notice"><%= notice %></p>

<table>
<tr>
    <th> ID </th>
    <th>Name </th>
    <th>Unit Price </th>
    <th> Quantity </th>
    <th>  Price </th>
    <th></th>
</tr>

  <%= render :partial => 'line_item' %>

 </table>
<span> Total </span><%= number_to_currency(Cart.get_total(session[:cart_id])) %>

局部视图

  <%  @lineitem.each do |item| %>
   <tr>
<td><%= item.id %>
    <td><%= item.product.name %></td>
   <td><%= number_to_currency(item.product.price) %></td>
     <td><%= item.quantity %></td>(Make this editable with update button)
    <td><%= number_to_currency(LineItem.get_price(item.id,item.product.price)) 
      %></td>
      <td><%= link_to 'Remove', item , confirm: 'Are you sure?', method: :delete  %></td>
        </tr>

         <% end %>
      <%= link_to 'Edit', edit_line_item_path %>


      **Thanks for the reply,**                                              

      <%  @line_item.each do |item| %>
     <tr>
<td><%= item.id %>
    <td><%= item.product.name %></td>
    <td><%= number_to_currency(item.product.price) %></td>
    <td><%= form_for(@line_item) do |f| %>

    <div class="field">
    <%= f.label :quantity %><br />
    <%= f.number_field  :quantity %>
    </div>
    <% end %>
 </td>
   <td><%=      number_to_currency(LineItem.get_price(item.id,item.product.price)) 
   %></td>
    <td><%= link_to 'Remove', item , confirm: 'Are you sure?', method: :delete  %></td>
    </tr>

       <% end %>

我如何使 item.quantity 可编辑,因为所有其他都是只读的?

4

1 回答 1

0

这条线

attr_accessible :cart_id, :product_id, :quantity

保护您的所有属性免于大规模分配,除了cart_id,product_idquantity. 这意味着除非您单独设置其他属性,否则它们将不可编辑。如果要允许其他属性可编辑,则必须将它们添加到该行。

@nathanvda 和 @simonmorley 是对的。您需要确保您提出问题并花时间让可能帮助您的其他人理解它。

于 2012-12-13T20:57:11.660 回答