我正在尝试从会话哈希中删除id。我能够将id推送到session[]
,但现在当我尝试将其从哈希中删除时,它会从camp.rb模型中完全删除id。我用脚手架创建了这个rails应用程序。有什么建议吗?
MODEL - cart.rb
class Cart
def initialize(array_of_camp_ids = [])
@camp_ids = array_of_camp_ids
end
def camps
Camp.where(id: @camp_ids)
end
_____________________________________________________________________
applicaition_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_cart
private
def set_cart
session[:cart] ||= []
@cart = Cart.new(session[:cart])
end
end
_____________________________________________________________________
cart_controller.rb
def add_to_compare
session[:cart] << @camp.id
redirect_to '/compare'
end
_____________________________________________________________________
VIEW - camps.html.rb
<p>
<strong>Name:</strong>
<%= @camp.name %>
</p>
<p>
<%= form_tag(action: "add_to_compare") do %>
<%= hidden_field :camp_id, value: @camp.id %>
<%= submit_tag "Add to comparison cart" %>
<% end %>
</p>
_____________________________________________________________________
VIEW - cart.html.erb
<tbody>
<% @cart.camps.each do |camp| %>
<tr>
<td><%= camp.name %></td>
<td><%= link_to 'remove', camp, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<tr>
<% end %>
</tbody>
答案 0 :(得分:0)
您的link_to正在调整购物车控制器上的销毁操作。默认情况下,将其从db中删除。如果您不想这样做,您可以session.data[:cart].delete @cart.id
。