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

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

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

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

Ruby on Rails

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

Q&A

2回答

1790閲覧

ネストさせているコントローラのルーティングがうまくいきません

swallowtail62

総合スコア6

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2017/05/21 01:55

編集2017/05/21 02:43

###前提・実現したいこと
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
となります。
どこのコードがおかしいのでしょうか?何卒よろしくお願いいたします。

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

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

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

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

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

guest

回答2

0

link_toのedit_my_thread_response_pathに2つの引数を入れているのにそれがうまく作用しません。

もう答えを自分自身で書いていると思いますが、

edit_my_thread_response GET /my_threads/:my_thread_id/responses/:id/edit(.:format) responses#edit

こうなっていて :my_thread_id は正しく入れているのに :id は間違った値を設定しようとしているからですね。

投稿2017/05/21 02:52

SugiTK

総合スコア495

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

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

swallowtail62

2017/05/21 02:57

``` <%= link_to "Edit", edit_my_thread_response_path([@my_thread, response]), class: "btn" %> ``` これで、:my_thread_id => @my_thread.id とはなるけど、 :id => response.id にはならないということでしょうか。 どういうコードにしたら正しい値が入るのかが分かりません。
SugiTK

2017/05/21 07:11

<%= link_to "Edit", edit_my_thread_response_path([@my_thread, response.id]), class: "btn" %> ではないのでしょうか?
swallowtail62

2017/05/21 09:34

以前もおっしゃるコードで試したことあるんですけど、同じ結果になるのですよね。。
guest

0

ルーティングがおかしいと考えていらっしゃるのなら、routes.rb や rails routes の結果を見せていただけませんか。

投稿2017/05/21 02:35

SugiTK

総合スコア495

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

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

swallowtail62

2017/05/21 02:44

ご指摘ありがとうございます。追記致しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問