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

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

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

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

Q&A

1回答

240閲覧

collection/showページにおいて画像をクリックするとそれに紐づくitem/show(rails routes→/items/:id(.:format) )へ遷移したい。

....t.i..a

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/15 07:26

#実現したい事
collection/showページにおいて画像をクリックするとそれに紐づくitem/show(rails routes→/items/:id(.:format) )へ遷移したい。

##現在
エラー表示がされる
ActiveRecord::RecordNotFound in CollectionsController#show
Couldn't find Item with 'id'=26

collectionにはid:26はありますがitemにはない状況です。

よろしくお願いいたします。

routes.rb

Rails.application.routes.draw do devise_for :users root 'pages#index' get "pages/show/:article_id", to: "pages#show" resources :collections resources :items resources :users, only:[:index, :show] do member do get :following, :followers end end resources :relationships, only: [:create, :destroy] get "items/:id" => "items#show" get "collections/:id" => "collections#show" end

collections/showのview

<div class= "container"> <div class= "logo_wrapper"> <%= link_to image_tag('logo.png', alt:"positive-learning", class: "service_logo"), '/' %> </div> <div class= "collection_wrapper"> <%= link_to item_path(@item) %> <%= @collection.image %> <%= @collection.explanation %> </div> </div>

collectionsのcontroller

class CollectionsController < ApplicationController before_action :transition_destination, only:[:new] def index @collections = Collection.includes(:user).order('created_at DESC') end def new end def create @collection = Collection.new(collection_params) if @collection.save redirect_to collections_path else render :new end end def edit # 編集時使用予定 # @collection = Collection.find(params[:image]) end def update end def show @item = Item.find(params[:id]) @collection = Collection.find(params[:id]) @collections = Collection.includes(:user).order('created_at DESC').first(5) end def destroy end private def collection_params params.require(:collection).permit(:name,:explanation,:user_id,:image) end def transition_destination @collection = Collection.new unless user_signed_in? redirect_to root_path end end end

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

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

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

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

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

guest

回答1

0

find(params[:id] って書けばどんな場合もデータが取れると思わないでください。万能の召喚呪文ではないです。
params[:id] が Itemのidではないから問題。

show を呼ぶ view と そのviewを書くcontrollerを載せてください

投稿2020/11/15 12:23

winterboum

総合スコア23416

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問