###前提・実現したいこと
Ruby on Rails5で掲示板を作成しています。
scaffoldを使ってスレッドの作成・編集・削除機能は実装できたのですが、
my_threadにネストさせたresponseの編集でつまづいています。
新規作成(create)は問題なくできたのですが、編集・削除はうまくいきません。
###発生している問題・エラーメッセージ
Couldn't find Response with 'id'=2 [WHERE `responses`.`my_thread_id` = ?] Request parameters {"controller"=>"responses", "action"=>"edit", "my_thread_id"=>"2/4", "id"=>"2"}
my_thread_id: 2, id: 4
のはずが上記のようになってしまいます。
###該当のソースコード
`response_controller.rb` class ResponsesController < ApplicationController def edit @my_thread = MyThread.find(params[:my_thread_id]) @response = @my_thread.responses.find(params[:id]) end
`my_thread_controller.rb` class MyThreadsController < ApplicationController before_action :set_my_thread, only: [:show, :edit, :update, :destroy] def show @response = @my_thread.responses.build end private def set_my_thread @my_thread = MyThread.find(params[:id]) end def my_thread_params params.require(:my_thread).permit(:title) end
`my_threads/show.html.erb` <p> <strong>Title:</strong> <%= @my_thread.title %> </p> <%= link_to 'Edit', edit_my_thread_path(@my_thread.id) %> | <%= link_to 'Back', my_threads_path %> <div class="responses"> <ul> <%= @my_thread.responses.each do |response| %> <li> <%= response.content %> <%= response.id %> <%= link_to "Edit", edit_my_thread_response_path([@my_thread, response]), class: "btn" %> <%= link_to "Delete", my_thread_response_path([@my_thread, response]), method: :delete, data: {confirm: "Are you sure?"}, class: "btn" %> </li> <% end %> </ul> </div> <div class="make-response"> <%= form_for([@my_thread, @response]) do |f| %> <%= f.text_field :content %> <%= f.submit "投稿" %> <% end %> </div>
`routes.rb` Rails.application.routes.draw do resources :my_threads do resources :responses end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
rake routesした結果 Prefix Verb URI Pattern Controller#Action my_thread_responses GET /my_threads/:my_thread_id/responses(.:format) responses#index POST /my_threads/:my_thread_id/responses(.:format) responses#create new_my_thread_response GET /my_threads/:my_thread_id/responses/new(.:format) responses#new edit_my_thread_response GET /my_threads/:my_thread_id/responses/:id/edit(.:format) responses#edit my_thread_response GET /my_threads/:my_thread_id/responses/:id(.:format) responses#show PATCH /my_threads/:my_thread_id/responses/:id(.:format) responses#update PUT /my_threads/:my_thread_id/responses/:id(.:format) responses#update DELETE /my_threads/:my_thread_id/responses/:id(.:format) responses#destroy my_threads GET /my_threads(.:format) my_threads#index POST /my_threads(.:format) my_threads#create new_my_thread GET /my_threads/new(.:format) my_threads#new edit_my_thread GET /my_threads/:id/edit(.:format) my_threads#edit my_thread GET /my_threads/:id(.:format) my_threads#show PATCH /my_threads/:id(.:format) my_threads#update PUT /my_threads/:id(.:format) my_threads#update DELETE /my_threads/:id(.:format) my_threads#destroy
###試したこと
show.html.erbの編集リンク(削除リンクも同様)からresponses_controller#editにルーティングがうまくいきません。
link_toのedit_my_thread_response_pathに2つの引数を入れているのにそれがうまく作用しません。
例えば、my_response_id: 1, id: 3のリンクに飛ぶと以下のようなurlになります。
http://localhost:3000/my_threads/1%2F3/responses/1/edit
my_response_id: 2, id: 4の場合は、
http://localhost:3000/my_threads/2%2F4/responses/2/edit
となります。
どこのコードがおかしいのでしょうか?何卒よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/21 02:57
2017/05/21 07:11
2017/05/21 09:34