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

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

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

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

Q&A

解決済

1回答

1836閲覧

.find(params[:id])が取得できません

goriwo

総合スコア17

Ruby on Rails

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

0グッド

0クリップ

投稿2021/02/20 14:16

前提・実現したいこと

find(params[:id])でidを取得して詳細表示させたい

発生している問題・エラーメッセージ

(prototypes_controller.rb) ActiveRecord::RecordNotFound in PrototypesController#index Couldn't find Prototype without an ID def show # binding.pry @prototype = Prototype.find(params[:id])ここでエラーになります end

該当のソースコード

prototypes_controller.rb class PrototypesController < ApplicationController before_action :show def index @prototypes = Prototype.all # binding.pry end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) # binding.pry if @prototype.save redirect_to root_path else render :new end end def show # binding.pry @prototype = Prototype.find(params[:id]) end private def prototype_params params.require(:prototype).permit(:title,:catch_copy,:concept,:image) .merge(user_id: current_user.id) end end
index.html.rb <main class="main"> <div class="inner"> <% if user_signed_in? %> <div class="greeting"> <%= "こんにちは" %> <%= link_to "#{current_user.name}さん", root_path, class: :greeting__link%> </div> <% else %> <div class="card__wrapper"> <% end %> <%= render partial: "prototype", collection: @prototypes %> </div> </div> </main>
_prototype.html.rb <div class="card"> <%= link_to image_tag(prototype.image, class: :card__img) , prototype_path(prototype.id) %> <div class="card__body"> <%= link_to prototype.title, prototype_path(prototype.id), class: :card__title%> <p class="card__summary"> <%= prototype.catch_copy %> </p> <%= link_to "by #{prototype.user.name}", root_path, class: :card__user %> </div> </div>
prototype.rb class Prototype < ApplicationRecord belongs_to :user has_one_attached :image validates :title, presence: true validates :catch_copy, presence: true validates :concept, presence: true validates :image, presence: true end
user.rb class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :prototypes validates :name, presence: true validates :profile, presence: true validates :occupation, presence: true validates :position, presence: true end
routes.rb Rails.application.routes.draw do devise_for :users get 'prototypes/index' # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root to: "prototypes#index" resources :prototypes, only: [:index,:new,:create,:show] end

試したこと

binding.pryで検証してparamsはnillだったのでユーザーモデルのバリデーションかえたけどうまくいかなかった

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

PrototypesControllerbefore_action :showは何のために書いたものでしょうか。

idが本来不要なindexなどのアクションでも、before_action :showがあることでshowが実行され、もちろんparams[:id]は存在しませんので、表示のようなエラーとなります。

投稿2021/02/20 14:34

maisumakun

総合スコア145183

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

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

goriwo

2021/02/20 14:39

before_action :showを手違いでいれてました エラー解決できました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問