我正在用 ruby on rails 实现一个购物车 购物车有两张表,即购物清单和产品。(产品引用购物清单)在购物清单中,当我单击选项获取产品时,它向我显示产品列表对于特定的购物清单。还有一个编辑和删除按钮。我已经实现了删除,如果我单击删除数据库中特定产品的数据库中删除的值变为 1(默认为零),但该产品仍显示在前端(我的是 HTML)。
理想情况下,我不希望特定产品显示在前端。你能帮帮我吗?我的 shopping_list 控制器如下:-
class ShoppingListsController < ApplicationController
# GET /shopping_lists
# GET /shopping_lists.json
def index
@shopping_lists = ShoppingList.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @shopping_lists }
end
end
# GET /shopping_lists/1
# GET /shopping_lists/1.json
def show
@shopping_list = ShoppingList.find(params[:id])
@shopping_list = ShoppingList.includes(:products).find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @shopping_list }
end
end
# GET /shopping_lists/new
# GET /shopping_lists/new.json
def new
@shopping_list = ShoppingList.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @shopping_list }
end
end
# GET /shopping_lists/1/edit
def edit
@shopping_list = ShoppingList.find(params[:id])
end
# POST /shopping_lists
# POST /shopping_lists.json
def create
@shopping_list = ShoppingList.new(params[:shopping_list])
respond_to do |format|
if @shopping_list.save
format.html { redirect_to @shopping_list, notice: 'Shopping list was successfully created.' }
format.json { render json: @shopping_list, status: :created, location: @shopping_list }
else
format.html { render action: "new" }
format.json { render json: @shopping_list.errors, status: :unprocessable_entity }
end
end
end
def list
@shopping_lists = ShoppingList.find(:all,
:order => "id asc")
respond_to do |format|
format.html { render :action => 'list' }
format.json { render :json => @accounts.to_json }
end
end
# PUT /shopping_lists/1
# PUT /shopping_lists/1.json
def update
@shopping_list = ShoppingList.find(params[:id])
respond_to do |format|
if @shopping_list.update_attributes(params[:shopping_list])
format.html { redirect_to @shopping_list, notice: 'Shopping list was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @shopping_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shopping_lists/1
# DELETE /shopping_lists/1.json
def delete
@shopping_list = ShoppingList.find(params[:id])
@shopping_list.deleted = 1
@shopping_list.save
respond_to do |format|
format.html { redirect_to shopping_lists_url }
format.json { render :json => { :success => true } }
# def destroy
# @shopping_list = ShoppingList.find(params[:id])
# @shopping_list.destroy
#respond_to do |format|
# format.html { redirect_to shopping_lists_url }
#format.json { head :no_content }
end
end
def get_product
@product=Product.where('shopping_list_id = ? ', params[:id])
# @status=Product.where(status=[[:Open, "open"], [:Close, "close"]])
# @product_status=Product.where('status = ? ', params[:id])
# @product=Product.find(params[:shopping_list_id])
# @products = Product.includes(:shopping_lists)
respond_to do |format|
format.html { render :action => 'get_products' }
format.json { render json: @shopping_lists }
end
end
def product_edit
@product = Product.find(params[:id])
end
def product_delete
@product = Product.find(params[:id])
@product.deleted = 1
@product.save
respond_to do |format|
format.html { redirect_to shopping_list_url }
format.json { render :json => { :success => true } }
end
end
def add_product
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @product }
end
end
end
我要显示的 Html 页面如下:-
<h1>shopping_list</h1>
shopping_list Id: <%= params[:id] %><br>
shopping_list Name: <%= ShoppingList.where('id = ?', params[:id]).pluck(:shopping_list_name)[0] %><br><br/>
<table border="1">
<tr><td>Product id.</td><td>Product Name</td><td>category</td><td>quantity</td><td>status</td><td>Product_edit</td><td>Product_delete</td></tr>
<% @product.each do |p| %>
<tr>
<td>Product id: <%= p.id %> <br></td>
<td>Product name: <%= p.product_name %> <br></td>
<td>Product category: <%= p.product_category %> <br></td>
<td>Product Quantity: <%= p.quantity %> <br></td>
<td>Product status: <%= p.status %>
<td><%= link_to 'Edit', {:action => 'product_edit', :id => p.id} %> </td>
<td><%= link_to 'Delete', {:action => 'product_delete', :id => p.id},
:data => { :confirm => "Are you sure you want to delete this product?" } %></td>
</tr>
</tr>
<% end %>
</table>
<br/>
<%= link_to 'Back', {:action => 'list'} %><br/>
<%= link_to 'Add New Products', {:action => 'add_product'} %>