ruby on rails でショッピング カートを実装しています。ショッピング カートには、ショッピング リストと製品の 2 つのテーブルがあります。特定の買い物リストのために。編集および削除ボタンもあります。削除をクリックすると、データベース内の特定の製品のデータベース内の削除の値が 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'} %>