簡體   English   中英

僅購物車數量可在滑軌上的紅寶石中編輯

[英]Cart only quantity editable in ruby on rails

我有一個購物車,在我的購物車頁面中,我試圖為用戶提供更新數量的靈活性,並且我只希望一個數量字段可編輯。 這是我的代碼

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在所有其他人均為只讀的情況下可編輯?

這條線

attr_accessible :cart_id, :product_id, :quantity

保護您的所有屬性免遭大量分配,但cart_idproduct_idquantity除外。 這意味着除非單獨設置其他屬性,否則其他屬性將不可編輯。 如果要允許其他屬性可編輯,則必須將它們添加到此行。

@nathanvda和@simonmorley是正確的。 您需要確保提出問題,並花一些時間讓其他可能會幫助您的人理解它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM