ECサイトを作っているのですが、deleteが効かないくて対処の仕方がわからず困っています。
前までは問題なくdeleteが動作していたのですが、久しぶりにdelete動作をさせたらできなくなっていました。
deleteボタンを押した後の反応は、何も変化がなくただリダイレクト先に飛ぶだけでした。問題の起きているdestroyが使ってあるコントローラー自体は触っていないので問題がわからず困っています
エラーの表示等は何もないのでdeleteボタんを押した際のターミナルの表示を載せます。
Started DELETE "/carts/206" for 10.0.2.2 at 2018-05-27 10:00:16 +0000 Processing by CartsController#destroy as HTML Parameters: {"authenticity_token"=>"4S6x2H5bgU2j/FTTLS6MM5vJswN92/HGHpEhrkbtv3zLdmbMY1jf5aHGoFNIQTbJWQCAlBxrgJEYBS1/N0s7fA==", "id"=>"206"} User Load (1.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 7], ["LIMIT", 1]] Redirected to http://localhost:3000/ Filter chain halted as :admin_user rendered or redirected Completed 302 Found in 7ms (ActiveRecord: 1.9ms)
一応deleteするコントローラーです
class CartsController < ApplicationController # before_action :set_cart, only: [:show, :edit, :update, :destroy] skip_before_action :authorize, only: [:create, :update, :destroy] before_action :admin_user def index @categories = Category.all @carts = Cart.all respond_to do |format| format.html # index.html.erb format.json { render json: @carts } end end def show begin @cart = Cart.find(params[:id]) rescue ActiveRecord::RecordNotFound logger.error "無効なカート#{params[:id]}にアクセスしようとしました" redirect_to store_url, notice: '無効なカートです' else respond_to do |format| format.html #show.html.erb format.json { render json: @cart } end end end def new @cart = Cart.new @categories = Category.all respond_to do |format| format.html # new.html.erb format.json { render json: @cart } end end def edit @cart = Cart.find(params[:id]) end def create @cart = Cart.new(params[:cart]) respond_to do |format| if @cart.save format.html { redirect_to @cart, notice: 'Cart was successfully created.' } format.json { render json: @cart, status: :created, location: @cart } else format.html { render :new } format.json { render json: @cart.errors, status: :unprocessable_entity } end end end def update @cart = Cart.find(params[:id]) respond_to do |format| if @cart.update_attributes(params[:cart]) format.html { redirect_to @cart, notice: 'Cart was successfully updated.' } format.json { head :ok } else format.html { render :edit } format.json { render json: @cart.errors, status: :unprocessable_entity } end end end def destroy @cart = current_cart @cart.destroy session[:cart_id] = nil respond_to do |format| format.html { redirect_to store_url } format.json { head :ok } end end # private # def set_cart # @cart = Cart.find(params[:id]) # end # def cart_params # params.fetch(:cart, {}) # end private def admin_user redirect_to(store_path) unless current_user.admin? end end
view/carts/_cart.html.erb
<table> <%= render(cart.line_items) %> <tr class="total_line"> <td colspan="2">合計</td> <td><%= @cart.total_count %> 点 </td> <td class="total_cell"><%= number_to_currency( @cart.total_price, :locale => 'jp') %></td> </tr> </table> <% unless cart.line_items.empty? %> <%= button_to 'お会計!', new_order_path, method: :get %> <%= button_to '買い物かごを空にする', @cart, method: :delete, data:{confirm: '本当によろしいですか?'} %> <% end %>
view/layouts/application.html.erb
<head> <title></title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tag %> <!-- jQuery --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!-- jQuery UI --> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"> </head>
application.jsの末尾
// //= require jquery //= require jquery-ui //= require rails-ujs //= require turbolinks //= require_tree .
以上なのですが何かお気付きのところや、必要なデータなどありましたおっしゃってください!宜しくお願いしますm(._.)m
回答1件
あなたの回答
tips
プレビュー