前提・実現したいこと
RailsでDMのアプリケーションを作成しています。
今、「/chats/:id(.:format)」のview上で、commentsコントローラ、commentモデルを使用してコメントを表示しています。
createアクションでコメントの作成、表示はできるのですが、削除機能をつける時、上記のURLのviewのcommentのidを取得してコメントを削除できる機能をつけたいのですが、そのidが取得方法がわかりません。
ご教授願います。
発生している問題・エラーメッセージ
「/chats/:id(.:format)」のview上で、commentモデルのidを取得し、削除を行える機能を実装したいが、そのidの取得方法がわからない。
Couldn't find Comment with 'id'=id
該当のソースコード
commentsコントローラ
Ruby
1class CommentsController < ApplicationController 2 def create 3 if Entry.where(:user_id => current_user.id, :chat_id => params[:comment][:chat_id]).present? 4 @comment = Comment.create((comment_params).merge(:user_id => current_user.id)) 5 redirect_to chat_path 6 else 7 redirect_back(fallback_location: chat_path) 8 end 9 end 10 11 def destroy 12 @comment = Comment.find(:id) 13 @comment.destroy 14 redirect_to chat_path 15 end 16 17 private 18 def comment_params 19 params.require(:comment).permit(:user_id,:content,:chat_id) 20 end 21 22end
chatsコントローラ
Ruby
1class ChatsController < ApplicationController 2 3 def create 4 @chat = Chat.create 5 @entry1 = Entry.create(:chat_id => @chat.id,:user_id => current_user.id) 6 @entry2 = Entry.create((entry_params).merge(:chat_id => @chat.id)) 7 redirect_to chat_path(@chat.id) 8 end 9 10 def show 11 @chat = Chat.find(params[:id]) 12 if Entry.where(:chat_id => @chat.id,:user_id => current_user.id).present? 13 @comments = @chat.comments 14 @comment = Comment.new 15 @entries = @chat.entries 16 else 17 redirect_back(fallback_location: root_path) 18 end 19 end 20 21 private 22 def entry_params 23 params.require(:entry).permit(:user_id,:chat_id) 24 end 25 26end
commentsのDB
Ruby
1class CreateComments < ActiveRecord::Migration[5.2] 2 def change 3 create_table :comments do |t| 4 t.references :user, foreign_key: true 5 t.references :chat, foreign_key: true 6 t.text :content 7 8 t.timestamps 9 end 10 end 11end
chatsコントローラのshowアクションのview
erb
1<%= link_to 'back to index',root_path %> 2<h1>Chat</h1> 3 4<h4>members</h4> 5<% @entries.each do |entries| %> 6<p><strong><%= entries.user.name %></strong></p> 7<p><strong><%= entries.user.email %></strong></p> 8<% end %> 9 10<% if @comment.present? %> 11 <% @comments.each do |comment| %> 12 <p><%= comment.content %></p> 13 <p>by <%= comment.user.name %> </p> 14 <%= link_to 'delete', comment_d_path(@comment), method: :delete %> 15 <% end %> 16<% else %> 17 <p>no message</p> 18<% end %> 19 20<%= form_for @comment,url:chat_path do |speak| %> 21 <%= speak.text_field :content ,:placeholder => 'Please enter the message here',:size => 70 %> 22 <%= speak.hidden_field :chat_id,:value => @chat.id %> 23 <%= speak.submit 'send' %> 24<% end %> 25
routs.rb(ターミナル上)
chats_show GET /chats/show(.:format) chats#show new_user_session GET /users/sign_in(.:format) users/sessions#new user_session POST /users/sign_in(.:format) users/sessions#create destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit user_registration PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy POST /users(.:format) users/registrations#create root GET / users#index users GET /users(.:format) users#index user GET /users/:id(.:format) users#show chats POST /users/:id(.:format) chats#create chat GET /chats/:id(.:format) chats#show comment POST /chats/:id(.:format) comments#create comment_d DELETE /chats/:id(.:format) comments#destroy refile_app /attachments #<Refile::App app_file="/home/vagrant/.bundle/ruby/2.5.0/refile-46b4178654e6/lib/refile/app.rb"> rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
試したこと
・commentsコントローラのdestroyアクションで、findメゾットの代わりにfind_byメゾットを使用したりして試行錯誤した。
(使いこなせていないので、ひょっとしたらこのメゾットで改善できるのかもしれませんが…)
補足情報(FW/ツールのバージョンなど)
ruby 2.5.3
Rails 5.2.3
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/10 04:05
退会済みユーザー
2019/04/10 07:00
2019/04/10 09:34
退会済みユーザー
2019/04/10 09:39
2019/04/10 12:29
退会済みユーザー
2019/04/11 01:05