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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

パス

パス(path)はファイルシステムの場所(階層)を明示したものです。

ルーティング

ルーティングとは、TCP/IPネットワークにおいて、目的のホストまでパケットを送る為のパス選定のプロセスを言います。

Q&A

解決済

1回答

2171閲覧

詳細ページから削除ボタンをクリックすると投稿が削除できるようしたい

yosida0929

総合スコア1

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

パス

パス(path)はファイルシステムの場所(階層)を明示したものです。

ルーティング

ルーティングとは、TCP/IPネットワークにおいて、目的のホストまでパケットを送る為のパス選定のプロセスを言います。

0グッド

0クリップ

投稿2021/01/22 18:28

編集2021/01/23 05:42

現在スクールの実装中でして、削除機能を作っています。
ある程度スクールの模範回答など解析して実装を進めているのですが、
投稿した内容を削除できません。

prototypes_controller.rb class PrototypesController < ApplicationController before_action :set_prototype, except: [:index, :new, :create] before_action :authenticate_user!, except: [:index, :show] before_action :contributor_confirmation, only: [:edit, :update, :destroy] def index @prototypes = Prototype.includes(:user) end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path(@prototype) else render :new end end def show # @comment = Comment.new # @comments = @prototype.comments.includes(:user) end def edit # @prototype = Prototype.find(params[:id]) end def update # prototype = Prototype.find(params[:id]) # prototype.update(prototype_params) if @prototype.update(prototype_params) redirect_to root_path(@prototype) else render :edit end end def destroy **ここが影響していると思うのですが。** if @prototype.destroy redirect_to root_path else redirect_to root_path end end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end def set_prototype @prototype = Prototype.find(params[:id]) end def contributor_confirmation redirect_to root_path unless current_user == @prototype.user end end
models/prototype.rb class Prototype < ApplicationRecord belongs_to :user has_one_attached :image has_many :commnets, dependent: :destroy validates :title, presence: true validates :catch_copy, presence: true validates :concept, presence: true validates :image, presence: true end
views/show.html.erb <main class="main"> <div class="inner"> <div class="prototype__wrapper"> <p class="prototype__hedding"> <%= @prototype.title %> </p> <%= link_to "by #{@prototype.user.name}", user_path(@prototype.user), class: :prototype__user %> <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> <% if current_user == @prototype.user%> <div class="prototype__manage"> <%= link_to "編集する", edit_prototype_path(@prototype), class: :prototype__btn %> <%= link_to "削除する", prototype_path(@prototype), method: :delete, class: :prototype__btn %> </div> <% end %> <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> <div class="prototype__image"> <%= image_tag @prototype.image %> </div> <div class="prototype__body"> <div class="prototype__detail"> <p class="detail__title">キャッチコピー</p> <p class="detail__message"> <%= @prototype.catch_copy %> </p> </div> <div class="prototype__detail"> <p class="detail__title">コンセプト</p> <p class="detail__message"> <%= @prototype.concept %> </p> </div> </div> <%# <div class="prototype__comments"> %> <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> <%# <% if user_signed_in? %> <%# <%= form_with model: [@prototype, @comment], local: true do |f|%> <%# <div class="field"> <%= f.label :text, "コメント" %><br /> <%# <%= f.text_field :text %> <%# </div> <div class="actions"> %> <%# <%= f.submit "送信する", class: :form__btn %> <%# </div> %> <%# <% end %> <%# // ログインしているユーザーには上記を表示する %> <%# <ul class="comments_lists"> <% @comments.each do |comment| %> <%# 投稿に紐づくコメントを一覧する処理を記述する %> <%# <li class="comments_list"> <%= comment.text%> <%# <%= link_to "(#{comment.user.name})", user_path(comment.user), class: :comment_user %> <%# </li> <% end %> <%# // 投稿に紐づくコメントを一覧する処理を記述する %> <%# </ul> %> </div> </div> </div> </main>
routes.rb Rails.application.routes.draw do devise_for :users root to: "prototypes#index" resources :prototypes, only: [:new, :create, :show, :edit, :update, :destroy] do resources :comments, only: :create end resources :users, only: :show end

エラーが出るわけではないのですが、削除されません。
ルーティングの設定
パスの指定
destroyアクションの定義
削除するレコードのidの指定
レコードの削除   など
したのですがなかなか解決できません。
どうか、よろしくお願い致します。

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

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

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

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

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

m.ts10806

2021/01/22 22:49

想定の場所を通っているか、デバッグしてみましたか?
yosida0929

2021/01/23 05:30

ありがとうございます。 デバッグしてみたのですが、通ってにようです。 となると、怪しいところはルーティングですか?
yosida0929

2021/01/23 05:39

``` routes.rb Rails.application.routes.draw do devise_for :users root to: "prototypes#index" resources :prototypes, only: [:new, :create, :show, :edit, :update, :destroy] do resources :comments, only: :create end resources :users, only: :show end ```
guest

回答1

0

ベストアンサー

@prototype.destroy ここでエラー出ませんか?
@prototype.destroy! にしてみて。

@prototypeが未定義なのです。
before_action :set_prototype, except: [:index, :new, :create]
ここを見なおして。

投稿2021/01/24 03:39

winterboum

総合スコア23401

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

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

yosida0929

2021/01/24 05:27

ありがとうございます。 試してみたのですが、ダメでした。。。。 そこでlayouts <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>の反応を確認した所、うまく反応していない事に気づいた為 環境構築をし直したことで直りました。 お手伝いしていただきありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問