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

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

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

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

Ruby on Rails

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

Q&A

0回答

707閲覧

ActionController::UrlGenerationError を解決できません

utsu001

総合スコア1

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/12/20 06:37

Railsで投稿したものを表示させるアプリを作っています。

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

ActionController::UrlGenerationError in Prototypes#index Showing app/views/prototypes/_prototype.html.erb where line #2 raised: No route matches {:action=>"show", :controller=>"prototypes"}, missing required keys: [:id]

該当のソースコード

ビュー
部分テンプレート_prototype

<div class="card"> <%= link_to image_tag(prototype.image, class: :card__img ), prototype_path %> <div class="card__body"> <%= link_to prototype.title, root_path,class: :card__title%> <p class="card__summary"> <%= prototype.catch_copy %>

index

<main class="main"> <div class="inner"> <%# ログインしているときは以下を表示する %> <% if user_signed_in? %> <div class="greeting"> こんにちは、 <%= link_to "#{current_user.name}さん", root_path, class: :greeting__link%> </div> <% end %> <%# // ログインしているときは上記を表示する %> <div class="card__wrapper"> <%# 投稿機能実装後、部分テンプレートでプロトタイプ投稿一覧を表示する %> <%= render partial: "prototype", collection: @prototypes %> </div> </div> </main>

コントローラー

class PrototypesController < ApplicationController def index @prototypes = Prototype.all end def new @prototype = Prototype.new end def create @prototype = Prototype.create(prototype_params) if @prototype.save redirect_to root_path else render prototypes: :form end end def show @prototype = Prototype.find(params[:id]) end def edit @prototype = Prototype.find(params[:id]) end def update end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end end

モデル

class Prototype < ApplicationRecord validates :title, presence: true validates :catch_copy, presence: true validates :concept, presence: true validates :image, presence: true belongs_to :user has_one_attached :image end

ルーティング

Rails.application.routes.draw do devise_for :users get 'prototypes/index' root to: "prototypes#index" resources :prototypes, only: [:index, :new, :create,:show, :edit, :update] end

試したこと

ビューの_prototype部分
<%= link_to image_tag(prototype.image, class: :card__img ), prototype_path %>
ここのprototype_pathにmehod: :getを付け足したりprototype.imageに@prototype.imageに変更をしました

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

rails 6.0.0

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問