質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

899閲覧

Couldn't find Circle with 'id'={:id=>" "}削除機能のエラーが解決しない

popi06

総合スコア3

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/04/11 12:20

編集2021/04/11 13:16

railsでチームの編集・削除機能を実装しようと思ったのですが編集機能は問題なく実装できたのですが、削除機能が下記のエラーが出てうまく機能しません。
初学者なのでどの点を修正した方が良いか教えていただきたいです。

発生したエラー

ActiveRecord::RecordNotFound in CirclesController#destroy Couldn't find Circle with 'id'={:id=>"3"} def destroy Circle.find(id: params[:id]) ⇦ if @circle.destroy flash[:success] = "サークルを削除しました" else flash[:danger] = '削除に失敗しました。' redirect_to circle_url end end ↑elseは失敗がわかりやすいように一時的に実装しています。
route resources :circles do resources :applies, only:[:index, :create, :destroy] resources :circle_users get :search, on: :collection collection do get 'top' get 'get_category_children', defaults: { format: 'json' } get 'get_category_grandchildren', defaults: { format: 'json' } get 'name_search' end end

show.html.erb

<% if @circle.owner? %>
<%= link_to "編集", edit_circle_path(@circle), class:"btn btn-warning btn-lg" %>
<%= link_to "削除", circle_path, method: :delete, class:"btn btn-warning btn-lg",
data: { confirm: "(確認)サークルが削除されます。" } %>
<% end %>

```ここに言語を入力 /app/controllers/circles_controller.rb class CirclesController < ApplicationController before_action :set_circle, only: [:update, :destroy, :edit] before_action :set_parents, only: [:new, :create, :destroy] before_action :owner_user, only: [:edit, :destroy] def index @circles = Circle.paginate(page: params[:page]).search(params[:search]) end def member @circle_user = CircleUser.paginate(page: params[:page]) end def new @circle = Circle.new @circle.user << current_user end def show @circle = Circle.find(params[:id]) if !@circle.user.include?(current_user) @circle.user << current_user end # @circleposts = @circle.circleposts.paginate(page: params[:page]) @circleposts = Circlepost.where(circle_id: @circle.id).all end def create @circle = Circle.new(circle_params) @circle.owner = current_user if @circle.save flash[:success] = "作成しました" redirect_to circle_users_path else render :new end end def edit @circle = Circle.find(params[:id]) end def update @circle = Circle.find(params[:id]) if @circle.update(circle_params) flash[:success] = "サークル情報が更新されました" redirect_to @circle else render 'edit' end end def destroy Circle.find(id: params[:id]) if @circle.destroy flash[:success] = "サークルを削除しました" else flash[:danger] = '削除に失敗しました。' redirect_to circle_url end end def search if params[:name].present? @circles = Circle.where('name LIKE ?', "%#{params[:name]}%") else @circles = Circle.none end end def set_parents @parents = Category.where(ancestry: nil) end def get_category_children @category_children = Category.find("#{params[:parent_id]}").children end def get_category_grandchildren @category_grandchildren = Category.find("#{params[:child_id]}").children end def top respond_to do |format| format.html format.json do if params[:parent_id] @childrens = Category.find(params[:parent_id]).children elsif params[:children_id] @grandChilds = Category.find(params[:children_id]).children elsif params[:gcchildren_id] @parents = Category.where(id: params[:gcchildren_id]) end end end end private def owner_user redirect_to(root_url) unless current_user && @circle.owner? end def circle_params params.require(:circle).permit(:name, :place, :time, :homepage, :profile, { :user_ids => [] }) end def set_circle @circle = Circle.find(params[:id]) end end
ログ Processing by CirclesController#destroy as HTML Parameters: {"authenticity_token"=>"2EfUYe9kp5Ug4FFnjd6KL5j1uy+shMgvTZ8Mvmy79PS+s10HCi7AOr8URZNuWpdKV7ACVPnmK3ecn3I3yNzGEQ==", "id"=>"3"} Circle Load (0.2ms) SELECT "circles".* FROM "circles" WHERE "circles"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] ↳ app/controllers/circles_controller.rb:109:in `set_circle' User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:18:in `current_user' Circle Load (0.1ms) SELECT "circles".* FROM "circles" WHERE "circles"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]] ↳ app/controllers/circles_controller.rb:54:in `destroy' Completed 404 Not Found in 36ms (ActiveRecord: 0.4ms | Allocations: 24892) ActiveRecord::RecordNotFound (Couldn't find Circle with 'id'={:id=>"3"}): app/controllers/circles_controller.rb:54:in `destroy' --- ### 試したこと dependent: :destroyを色々と変えてみたものの、うまくいきませんでした

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

def destroy
@circle = Circle.find_by(id: params[:id])
return redirect_to :root if @circle.nil?
@circle.destroy
flash[:success] = 'サークルを削除しました。'
redirect_to :root
end

↑このように書き換えたら削除できるようになりました。

投稿2021/04/11 15:28

popi06

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問