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

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

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

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

Q&A

解決済

1回答

1291閲覧

Couldn't find Prototype without an IDを解決したい

goriwo

総合スコア17

Ruby on Rails

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

0グッド

0クリップ

投稿2021/02/20 09:29

編集2021/02/20 13:00

前提・実現したいこと

(params[:id])でidを取得したいです

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

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

該当のソースコード

(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
(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.erb) <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>
(show.html.erb) <main class="main"> <div class="inner"> <div class="prototype__wrapper"> <p class="prototype__hedding"> <%= prototype.title %> </p> <%= link_to "by #{prototype.user.name}", prototype_path(prototype.user_id), class: :prototype__user %> <div class="prototype__manage"> <%= link_to "編集する", root_path, class: :prototype__btn %> <%= link_to "削除する", root_path, class: :prototype__btn %> </div> <div class="prototype__image"> <%= image_tag prototype.image %> </div> <div class="prototype__body"> <div class="prototype__detail"> <p class="detail__title">キャッチコピー</p> <p class="detail__message"> <%= prototype.catch_copy %> </p> </div> <div class="prototype__detail"> <p class="detail__title">コンセプト</p> <p class="detail__message"> <%= prototype.concept %> </p> </div> </div> <div class="prototype__comments"> <%# <%= form_with local: true do |f|%> <div class="field"> <%# <%= f.label :hoge, "コメント" %><br /> <%# <%= f.text_field :hoge %> </div> <div class="actions"> <%# <%= f.submit "送信する", class: :form__btn %> </div> <%# <% end %> <ul class="comments_lists"> <li class="comments_list"> <%# <%= " コメントのテキスト "%> <%# <%= link_to "( ユーザー名 )", root_path, class: :comment_user %> </li> </ul> </div> </div> </div> </main>
(_prototype.html.erb) <div class="card"> <%= link_to image_tag(prototype.image, class: :card__img) , prototype_path(prototype.user_id) %> <div class="card__body"> <%= link_to prototype.title, prototype_path(prototype.user_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>

試したこと

(params[:id])の中がnillだったのでprototype.user_id又はprototype.idで送れると思ったのですがだめでした

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

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

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

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

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

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

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

guest

回答1

0

自己解決

:user.rbにバリエーションしてなかったので保存されなかったみたいです
validates :name, presence: true
validates :profile, presence: true
validates :occupation, presence: true
validates :position, presence: true

投稿2021/02/20 13:36

goriwo

総合スコア17

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問