Home>

When a delete button is added to each element extracted from the@cartarray, I want to be able to delete each corresponding element by pressing the button.

Error message

For@ cart.update

NoMethodError in CartController # update
undefined method `update 'for #<Array: 0x00007f8d82d455f0>


@ cart.delete ([: id])
No change.
In terminal, it looks like this:
Even if you use the delete/delete_at method, an error does not occur in rails, but the deleted content is not reflected.

Started PATCH "/ cart/1" for 127.0.0.1 at 2018-08-25 16:19:19 +0900
Processing by CartController # update as HTML
  Parameters: {"authenticity_token" =>"uCSoRxnlGlRIT/Z4YHFcppIA91GQpnIqbLXMtTZnOnsFi + 1X4TNMGN49eBZO + qzgd + kCcZovUw48uosD2pdLjQ ==", "id" =>"1"}
  Item Load (0.4ms) SELECT "items". * FROM "items" WHERE "items". "Id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/cart_controller.rb: 26
  CACHE Item Load (0.0ms) SELECT "items". * FROM "items" WHERE "items". "Id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/cart_controller.rb: 26
  Item Load (0.4ms) SELECT "items". * FROM "items" WHERE "items". "Id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]]
  ↳ app/controllers/cart_controller.rb: 26
Redirected to http: // localhost: 3000/cart
Completed 302 Found in 5ms (ActiveRecord: 0.8ms)
Applicable source code

cart.controller.rb

class CartController<ApplicationController
  # before_action: set_cart, only: [: update]
  def index
    if session [: cart]! = nil
      session [: cart] .sort!
      @cart = session [: cart] .map {| item_id | Item.find (item_id)}
    end
  end
  def create
    session [: cart] = [] unless session [: cart]
    session [: cart]<<params [: item_id]
    redirect_to top_path, notice: 'added items to cart'
  end
  def update
    # Remove a specific element from the array
    # session.delete ([: item_id])
    # @ cart.update
    # session [: cart] .delete ([: id])
    # @ cart.delete ([: id])
    @cart = session [: cart] .map {| item_id | Item.find (item_id)}
    @ cart.delete
    redirect_to cart_index_path
    # session [: cart]
    # =>["1", "1", "3", "3"]
    # @ cart
    # =>[#<Item id: 1, name: "1", description: "1", price: 1, created_at: "2018-08-23 09:53:01", updated_at: "2018-08 -23 09:53:01 ", image:" masamune.jpg "&gt ;, #<Item id: 1, name:" 1 ", description:" 1 ", price: 1, created_at:" 2018-08- 23 09:53:01 ", updated_at:" 2018-08-23 09:53:01 ", image:" masamune.jpg "&gt ;, #<Item id: 3, name:" 2 ", description:" 2 ", price: 2, created_at:" 2018-08-23 09:54:19 ", updated_at:" 2018-08-23 09:54:19 ", image:" transcendence.jpg "&gt ;, #<Item id: 3, name: "2", description: "2", price: 2, created_at: "2018-08-23 09:54:19", updated_at: "2018-08-23 09:54:19" , image: "transcendence.jpg">]
  end
  def destroy
    reset_session
    # @cart = session [: cart] .map {| item_id | Item.find (item_id)}
    # @ cart.destroy
    # @cart = Item.find (@item_id)
    # @ cart.destroy
    redirect_to cart_index_pathend
  # private
  # # Use callbacks to share common setup or constraints between actions.
  # def set_cart
  # @cart = session [: cart] .map {| item_id | Item.find (item_id)}
  # end
  #
  # # Never trust parameters from the scary internet, only allow the white list through.
  # def cart_params
  # params.require (: cart)
  # end
end

index.html.rb

<tbody>
<h1>Order List</h1>
<% sum = 0%>
<% if @ cart.blank?%>
<% = "Cart is empty"%><br>
<% else%>
<% @ cart.each do | item |%>
<tr>
<td>Item number:<% = item.id%></td>
<td>Price:<% = item.price%>yen</td><br>
<% sum + = item.price%>
<% = button_to "Clear Product", cart_path (item), method:: patch, data: {confirm: 'Are you sure?'}%><br>
<% end%><br>
<!-Total amount:<% = sum%>yen<br>->
</tr>
</tbody>
<br>
<% end%>
Tried

The contents are commented out in the source code.

# session.delete ([: item_id])
    # @ cart.update
    # session [: cart] .delete ([: id])
    # @ cart.delete ([: id])
Supplemental information (FW/tool version etc.)

Rails 5.2

Append

At index.html.erb

<% if @ cart.blank?%>
<% = "Cart is empty"%><br>
<% else%>
<% @ cart.each do | item |%>
<tr>
<td>Item number:<% = item.id%></td>
<td>Price:<% = item.price%>yen</td><br>
<% sum + = item.price%>
<% = button_to "Clear Product", cart_path (item), method:: patch, data: {confirm: 'Are you sure?'}%><br>
<% end%><br>


The data sent with cart_path (item) is@cart = session [: cart] .map {| item_id | Item. defined incart.controller def index.@cartdefined indef updateis entered instead of@cartin find (item_id)}.
And since Cart is not a model, methods such asdestroy, updateare not inherited and cannot be used.
However, by changing the description to@cart = Item.find (params [: id]), executing@ cart.destroywill execute the destruction of the Item model. Then delete the original data again.
Instead,@cartpulls the data I want to delete. .
I understand so far.

  • Answer # 1

    Looking at the code,@cartis a duplicate of the session entry, as it was obtained bymap.
    I think you need to write to session again after deleting.

Related articles