LoginSignup
1
1

More than 3 years have passed since last update.

ネストしたものに削除機能導入

Posted at

gorupの中のmessageに削除機能導入

ネストの中のmessageに削除機能導入するさい躓いたので備忘録として書きます。

routes.rb
  resources :groups, only: [:new, :create, :edit, :update, :show] do
    resources :messages, only: [:index, :create, :destroy]
  end
messages_controller.rb
  before_action :set_group
...
  def destroy
    message = Message.find(params[:id])
    message.destroy
    redirect_to group_messages_path(@groups)
  end
...
  def set_group
    @groups = Group.find(params[:group_id])
  end

rails routes
Image from Gyazo

問題点

躓いた箇所は下記のlink_to後ろの記述
このままではどのグループのどのメッセージなのか指定できてない

messages/index.html.erb
      <%= link_to "/messages/#{message.id}", method: :delete do%>

下記のようにメッセージの前にどのグループかを指定することで解決

messages/index.html.erb
      <%= link_to "/groups/#{@groups.id}/messages/#{message.id}", method: :delete do%>

rails routesをした時にURI Patternを見ておく!!!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1