恥ずかしながら初歩的なRouting Errorにハマってしまいました
メッセージの削除機能をつけています
削除ボタンを押したらエラーになってしまいます
自分の投稿だけ削除できるような記述をしています
宜しくお願いします
route.rb
6行目がメッセージのルーティングです
Rails.application.routes.draw do devise_for :users root 'groups#index' resources :users, only: [:index, :edit, :update] resources :groups, only: [:new, :create, :edit, :update, :destroy] do resources :messages, only: [:index, :create, :destroy] namespace :api do resources :messages, only: :index, defaults: { format: 'json' } end end end
haml
link_toの削除でdeleteのパスを指定しています
.message{data: {id: message.id}} .message__upper-info %p.face %p.message__upper-info__talker = message.user.name %p.message__upper-info__date = message.created_at.strftime("%Y/%m/%d %H:%M") = link_to '削除', "/groups/#{@group.id}/messages/#{@message.id}", method: :delete - if message.content.present? %p.message__text = message.content = image_tag message.image.url, class: 'lower-message__image' if message.image.present?
controller
destroyが削除機能です
class MessagesController < ApplicationController before_action :set_group def index @message = Message.new @messages = @group.messages.includes(:user) end def create @message = @group.messages.new(message_params) if @message.save respond_to do |format| format.html { redirect_to groups_messages_path(params[:group_id]) } format.json end end end def destroy message = Message.find(params[:id]) if message.user_id == current_user.id message.destroy end end private def message_params params.require(:message).permit(:content, :image).merge(user_id: current_user.id) end def set_group @group = Group.find(params[:group_id]) end end
rake routes
Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/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) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / groups#index users GET /users(.:format) users#index edit_user GET /users/:id/edit(.:format) users#edit user PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update group_messages GET /groups/:group_id/messages(.:format) messages#index POST /groups/:group_id/messages(.:format) messages#create group_message DELETE /groups/:group_id/messages/:id(.:format) messages#destroy group_api_messages GET /groups/:group_id/api/messages(.:format) api/messages#index {:format=>"json"} groups POST /groups(.:format) groups#create new_group GET /groups/new(.:format) groups#new edit_group GET /groups/:id/edit(.:format) groups#edit group PATCH /groups/:id(.:format) groups#update PUT /groups/:id(.:format) groups#update DELETE /groups/:id(.:format) groups#destroy
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/14 08:31