#実現したい事
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
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。