学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails5.1 | 24 | Webアプリケーション開発 | 買い物カートの削除機能

<<前  [TOP]  次>>


買い物カートから、商品を削除出来る機能を実装していきます。
まずは「line_items_controller.rb」の編集です。


【app/controllers/line_items_controller.rb】

class LineItemsController < ApplicationController
  before_action :set_line_item, only: [:show, :edit, :update, :destroy]

  # GET /line_items
  # GET /line_items.json
  def index
    @line_items = LineItem.all
  end

  # GET /line_items/1
  # GET /line_items/1.json
  def show
  end

  # GET /line_items/new
  def new
    @line_item = LineItem.new
  end

  # GET /line_items/1/edit
  def edit
  end

  # POST /line_items
  # POST /line_items.json
  def create

	@cart = current_cart

	good = Good.find(params[:good_id])

	@line_item = @cart.add_good(good.id)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to @line_item.cart, notice: 'カートに商品が追加されました。' }
        format.json { render :show, status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /line_items/1
  # PATCH/PUT /line_items/1.json
  def update
    respond_to do |format|
      if @line_item.update(line_item_params)
        format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
        format.json { render :show, status: :ok, location: @line_item }
      else
        format.html { render :edit }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /line_items/1
  # DELETE /line_items/1.json
  def destroy
    @line_item.destroy

    respond_to do |format|
      format.html { redirect_to cart_url(@line_item.cart_id), notice: '商品をカートから削除しました。' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_line_item
      @line_item = LineItem.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def line_item_params
      params.require(:line_item).permit(:good_id, :cart_id)
    end
end



変更したのは「destroy()」メソッドです。

  def destroy
    @line_item.destroy

    respond_to do |format|
      format.html { redirect_to cart_url(@line_item.cart_id), notice: '商品をカートから削除しました。' }
      format.json { head :no_content }
    end
  end

「 redirect_to」で「cart_url」でリダイレクトし、「@line_item.cart_id」として商品のidを渡しています。
この「@line_item.cart_id」のところを「session[:cart_id]」としても上手くいきます。


次にcartsビューの「show.html.erb」ファイルを編集します。


【app/views/carts/show.html.erb】

<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<br>
<h1>Railsはじめてマート</h1>
<h2>   カートに追加された商品</h2>
<br>
<%= button_to 'カートを空にする', @cart, method: :delete, data: {confirm: 'カートを空にして本当によろしいですか?'}, class: 'fbtn' %>
<br>
<table>
	<tr>
		<th></th>
		<th>商品名</th>
		<th>価格</th>
		<th>数量</th>
		<th>合計</th>
		<th></th>

	</tr>
		<% @cart.line_items.each do |item| %>
		<tr>
			<td text-align:center;><img height="80" src="<%=h item.good.image_url %>"/></td>
			<td text-align:center;><font size="4"><%= item.good.title %></font></td>
			<td text-align:center;><font size="4"><%= (item.good.price).to_i %></font></td>
			<td text-align:center;><font size="4"><%= item.quantity %></font></td>
			<td text-align:center;><font size="4"><%= (item.total_price).to_i %></font></td>
			<td><%= button_to '削除', item, method: :delete, data: {confirm: 'カートから削除してよろしいですか?'}%></td>
		</tr>
		<% end %>

		<tr>
			<td colspan="6"><font size="5">総計:<%= (@cart.total_price).to_i %></font></td>
		</tr>

</table>
<br>
<span>  </span><%= link_to '買い物を続ける', market_path, :class => 'btn' %><span>



「削除」ボタンを追加しています。

<td><%= button_to '削除', item, method: :delete, data: {confirm: 'カートから削除してよろしいですか?'}%></td>



これでカートの商品を削除することが出来るようになりました。





↓↓クリックして頂けると励みになります。


<<前  [TOP]  次>>